Reputation: 1002
I have some code html:
<button>click 1</button>
<button>click 2</button>
And jQuery code :
jQuery(document).ready(function(){
$("button").click(function(){
console.log('yeah');
$(this).siblings().on('click');
$(this).off('click');
});
});
Something I wish:
it 's also the same instead of first, I click on button 2
But I wonder why it don't work.
Upvotes: 2
Views: 491
Reputation: 123739
You probably are looking for this:-
$(document).ready(function(){
$("button").on('click', handleClick);
});
function handleClick()
{
$(this).siblings().on('click', handleClick);
$(this).off('click');
console.log('yeah');
}
Another approach is to make use of one
$(document).ready(function(){
$("button").one('click', handleClick);
});
function handleClick(e)
{
e.stopImmediatePropagation();
$(this).siblings().one('click', handleClick);
console.log('yeah');
}
Update to avoid multiple click attached:-
$(document).ready(function () {
$("button").on('click', handleClick);
});
function handleClick(e) {
$(this).off('click').siblings().off('click').on('click', handleClick); //turn off the handler attached to the siblings (during initial load) before attaching it again.
console.log('yeah');
}
Upvotes: 2