I'am trying to change "onclick" attribute in jQuery but it doesn't change, here is my code:
$('#stop').click(function() {
$('next').attr('onclick','stopMoving()');
}
I have an element with id="stop" and when user clicks on it I want to change an onclick attribute on element which has id="next".
If someone knows where is the solution please help!
Do it the jQuery way (and fix the errors):
$('#stop').click(function() { $('#next').click(stopMoving); // ^-- missing # }); // <-- missing );
If the element already has a click
handler attached via the onclick
attribute, you have to remove it:
$('#next').attr('onclick', '');
Update: As @Drackir pointed out, you might also have to call $('#next').unbind('click');
in order to remove other click handlers attached via jQuery.
But this is guessing here. As always: More information => better answers.
As @Richard pointed out above, the onClick needs to have a capital 'C'.
$('#stop').click(function() {
$('next').attr('onClick','stopMoving()');
}
The easyest way is to change .attr() function to a javascript function .setAttribute()
$('#stop').click(function() {
$('next')[0].setAttribute('onclick','stopMoving()');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With