开发者

Find the next textarea using Jquery

HTML

<tr id="rowId"><td><textarea class="inputTextarea"></textarea></td><td><textarea class="inputTextarea"></textarea></td></tr>
<tr id="rowId2"><td><textarea class="in开发者_开发问答putTextarea"></textarea></td><td><textarea class="inputTextarea"></textarea></td></tr>
<tr id="rowId3"><td><textarea class="inputTextarea"></textarea></td><td><textarea class="inputTextarea"></textarea></td></tr>

Provided I know rowId, how do I find the next textarea ON THIS PAGE, starting at any arbitary point. I don't mean ANY input, textarea only. I need to be able to start AT ANY row, and progress on to the next textarea, essentially going down the rows.

EDIT Based on answers, I used the following code to traverse the textareas row by row:

var curElt = $('#' + startAt);  //startAt is the first row id   

        for(var i=1; i < 10; i++) {

            $(curElt).find('textarea').eq(0).val(i);
            $(curElt).find('textarea').eq(1).val(i+1);

            curElt = $(curElt).next();
        }


You can use the next and find methods:

$('#' + current_row_id).next().find('textarea').eq(0);

next will get the next sibling, and then find will find all of the elements within the set that match the passed-in selector(s), then eq(0) will grab the first element in the resulting set returned from find.


$('#rowId2').find('textarea');

That will find both children of the row. If you want the first one, either:

$('#rowId2').find('textarea').eq(0); //jQuery object
$('#rowId2').find('textarea')[0];    //DOM Element

Edit: If you only know one row and want to find the first textarea in the next row:

$('#rowId').next().find('textarea').eq(0);


$textarea = $('#rowId textarea').eq(0);

$nextarea = $textarea.closest('tr').next().find('textarea').eq(0);

Just to clarify, $.fn.next() finds the next sibling in the DOM.

  1. Starting from the textarea, first you have to find its parent-tr ( $textarea.closest('tr') ).
  2. From there, use next to find the next tr ( .next() )
  3. Finally, find the first textarea within that tr ( .find('textarea').eq(0) )


Try this:

$("#rowID textarea:first-child").val()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜