开发者

How to select a certain element that is closest to the end of a given div with jQuery?

开发者 https://www.devze.com 2023-02-24 03:41 出处:网络
Let\'s say I have a div called the_div. In this div there will be multiple submit bu开发者_StackOverflow社区ttons. The submit buttons are added dynamically on the page.

Let's say I have a div called the_div. In this div there will be multiple submit bu开发者_StackOverflow社区ttons. The submit buttons are added dynamically on the page.

<div id="the_div">
    ...other stuff
    <input type='submit' value='Add'>
    ...other stuff
    <input type='submit' value='Add'>
    ...other stuff
    <input type='submit' value='Add'> -need to select this one
</div>

Is there a way I can use a jQuery selector to select the submit button that is closest to the end of the div? In the example above it would be the third submit button.


Use last.

$('#the_div :submit:last')...


$('#the_div').find('input:submit').eq(-1)

This will select the last submit inside #the_div. eq() gives you allot of control. If you want to select the first submit you can do eq(0), or before last you can do eq(-2)

Check working example at http://jsfiddle.net/cvcHf/


YOU CAN TRY LIKE THIS

$("#THE_DIV").closest() or $('#the_div').find('input:submit').eq(':last');

PLEASE LET ME KNOW IF IT WAS NOT WORKING.. TH

0

精彩评论

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

关注公众号