4

I have this HTML structure

<div class="buttons">
    <button data-icon="ui-icon-disk" class="save">Save1</button>
    <button data-icon="ui-icon-check" class="ok">OK1</button>
    <button data-icon="ui-icon-trash" class="delete">Delete1</button>
    <button data-icon="ui-icon-close" class="close">Close1</button>
</div>

There are many blocks in the page like this one.
Some buttons come with click handlers functions (save and delete buttons).

What I want to do is: if someone click on an ok button, the corresponding save button click bound function should run.

My code:

$('.save').click(function(){
    alert('save');
});
$('.ok').click(function(){
    $('.save').click();
});

This is wrong, when I click on a ok button all save buttons fires... not only the one within the same buttons group.

Demo illustrating my problem.

1

3 Answers 3

5

Use $(this).siblings('.save').click() to select only siblings of the clicked button instead of all matching buttons in the whole document.

Demo: http://jsfiddle.net/ThiefMaster/nMnm7/2/
Docs: http://api.jquery.com/siblings/

Sign up to request clarification or add additional context in comments.

Comments

4

use siblings

$('.ok').click(function(){
    $(this).siblings('.save').click();
});

DEMO

Comments

1

http://api.jquery.com/siblings/

$('.ok').click(function(){
   $(this).siblings(".save").click();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.