开发者

How to show closest button in jQuery without using the closest() method?

I have a bunch of inputs and buttons, similar to how Facebook's news feed:

<div class="section_1">
    &开发者_开发问答lt;input name="input-name"/>
    <button style="display:none">Save</button>
</div>

I have a bunch of these (section_2, section_3, etc.). I could put id's like this:

<div class="section_1">
    <input class="expandableinput" name="input-name" sectionId="1" />
    <button style="display:none" sectionId="1">Save</button>
</div>

And show the button based on the section id (and this is just quick pseudo-code, forgive me if there's a mistake):

$('expandableinput').click(function() {
  var thissectionId = $(this).attr('sectionId');
  $('button[sectionId=thissectionId]').show();
});

Is there a better/faster way of accomplishing this, without using closest()?


You can use .next() to get the next sibling element, like this:

$('.expandableinput').click(function() {
  $(this).next().show();
});

This goes off your example using a class, though whatever selector you want use to identify the <input> would work as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜