purpletree
purpletree

Reputation: 1943

jQuery click next button

I have multiple buttons on a page and I want to simulate a click on the NEXT button.

$('input, select').keydown(function(e){
    if(e.which == 13)
    //$('input[type*=button]').click();
    $(this).next('input[type*=button]').click();
});

Html:

<tr>
    <td class="cell">
        <input type="text" name="domain" class="entryfield" value="http://">
    </td> 
</tr> 
<tr> 
    <td class="cell" align="right" colspan="2">
        <input type="button" class="button" value="Get IP" id="submitDomain"/>
    </td>
</tr>

The commented code works, but that clicks all buttons on the page.

Any help is appreciated, thanks.

Upvotes: 1

Views: 6004

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126072

Given your markup:

$("input, select").keydown(function() {
    $(this).closest("tr").next().find("input:button").click();
});

Basically, find the closest table row, then find its immediate sibling tr. Inside that tr find the button you'd like to click.

Next finds the immediately following sibling, so calling next on the input that the event occurred on wouldn't actually find anything.

Example: http://jsfiddle.net/N3WBP/

Upvotes: 2

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15876

try this:

$(this).next().click();

like here: http://api.jquery.com/next/

Upvotes: 0

Related Questions