35

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!

YoannFleuryDev
  • 737
  • 1
  • 10
  • 20
Shark
  • 1,093
  • 4
  • 11
  • 20
  • 2
    To select by `id` you have to prefix the id-name with a `#`, to give the selector `$('#next')`, which you did in the first selector but omitted in the second. – David says reinstate Monica Apr 25 '11 at 18:25
  • 2
    Also, I've found that .attr('onclick') doesn't find the attribute, whereas .attr('onClick') does--even though the attribute has the lower case 'c' in the html. – Richard Oct 11 '12 at 15:18

4 Answers4

55

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.

McGarnagle
  • 96,448
  • 30
  • 213
  • 255
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
  • Not enough info to be sure, but the OP is probably better off with `$('#next').one('click', stopMoving)` – mikerobi Apr 25 '11 at 18:26
  • 1
    You should use: `$('#next').unbind('click');` Also, you have "onlick" instead of "onclick". :) – Richard Marskell - Drackir Apr 25 '11 at 18:27
  • 1
    @mikerobi: I think you mean `.one('click', stopMoving)`. Yeah, maybe.... we don't know. – Felix Kling Apr 25 '11 at 18:28
  • 1
    it couldn't solve my broblem i need the way to change "onclick" attribute via attr('element','value') – Shark Apr 25 '11 at 18:29
  • @ShakoKakauridze: Could you create a demo? The code above will work. You have to explain what exactly did not work the way it should. – Felix Kling Apr 25 '11 at 18:32
  • 1
    use the code as Felix posted. Using jQuery you should not think of changing the onclick attribute of an element, but of attaching a click event listener to the element, which is done via the jQuery click() method. – Peter Perháč Apr 25 '11 at 18:26
  • 3
    The problem with these sorts of answers is they don't answer the question. These people don't realise that sometimes you are dealing with other people's code. Of course normally you would use the click function in jQuery. OF COURSE. But, I've a situation where I need to append something to the onClick event that is pre-exisitng. There are variables hardcoded into the HTML. I can't change the HTML. – Chris Harrison Feb 21 '13 at 06:13
14

As @Richard pointed out above, the onClick needs to have a capital 'C'.

$('#stop').click(function() {
     $('next').attr('onClick','stopMoving()');
}
Tim
  • 833
  • 7
  • 18
7

The easyest way is to change .attr() function to a javascript function .setAttribute()

$('#stop').click(function() {
    $('next')[0].setAttribute('onclick','stopMoving()');
}
Artnik
  • 71
  • 1
  • 1
0

Felix Kling's way will work, (actually beat me to the punch), but I was also going to suggest to use

$('#next').die().live('click', stopMoving);

this might be a better way to do it if you run into problems and strange behaviors when the element is clicked multiple times.

rahul
  • 7,048
  • 5
  • 33
  • 46
Ryan Sullivan
  • 397
  • 2
  • 5
  • 15