JavaScript: changing the value of onclick with or without jQuery?

You shouldn't be using onClick any more if you are using jQuery. JQuery provides its own methods of attaching and binding events. See click().

You shouldn't be using onClick any more if you are using jQuery. JQuery provides its own methods of attaching and binding events. See .click() $(document).

Ready(function(){ var js = "alert('B:' + this. Id); return false;"; // create a function from the "js" string var newclick = new Function(js); // clears onclick then sets click using jQuery $("#anchor"). Attr('onclick', '').

Click(newclick); }); That should cancel the onClick function - and keep your "javascript from a string" as well. The best thing to do would be to remove the onclick="" from the element in the HTML code and switch to using the Unobtrusive method of binding an even Using onclick = function() { return eval(js); } doesn't work because you are not allowed to use return in code passed to eval(). No - it won't, but onclick = eval("(function(){"+js+"})"); will wrap the 'js' variable in a function enclosure.

Onclick = new Function(js); works as well and is a little cleaner to read. (note the capital F) -- see documentation on Function() constructors.

2 attr('onclick', '') doesn't work, you'll need to use removeAttr('onclick') – David Lawson Jan 3 '11 at 4:56 Beware of this as well: jQuery unbind() vs removeAttr() see novogeek.com/post/2009/05/18/… – benjaminlotan Mar 6 '11 at 19:04 1 This has saved my life, been working at it for hours! – Abe Petrillo May 25 '11 at 14:12.

One gotcha with Jquery is that the click function do not acknowledge the hand coded onclick from the html. So, you pretty much have to choose. Set up all your handlers in the init function or all of them in html.

The click event in JQuery is the click function $("myelt"). Click (function ....).

Tom, I can't use $("myelt").click() here because the code I want to run is in a string. (See my comments to other answers here as well. ) – avernet May 5 '09 at 20:10 Ah.

I see how about: function js() { alert('B'); return false; } – Tom Hubbard May 5 '09 at 20:23.

Just use jQuery bind method! Jquery-selector!. Bind('event',!

Fn! ); See here for more about events in jQuery.

If you don't want to actually navigate to a new page you can also have your anchor somewhere on the page like this. And then to assign your string of JavaScript to the the onclick of the anchor, put this somewhere else (i.e. The header, later in the body, whatever): If you have all of this info on the server before sending out the page, then you could also simply place the JavaScript directly in the href attribute of the anchor like so: Click me.

Note that following gnarf's idea you can also do: var js = "alert('B:' + this. Id); return false;"; var newclick = eval("(function(){"+js+"});"); $("a"). Get(0).

Onclick = newclick; That will set the onclick without triggering the event (had the same problem here and it took me some time to find out).

BTW, without JQuery this could also be done, but obviously it's pretty ugly as it only considers IE/non-IE: if(isie) tmpobject. SetAttribute('onclick',(new Function(tmp.nextSibling. GetAttributeNode('onclick').

Value))); else $(tmpobject). Attr('onclick',tmp.nextSibling. Attributes0.

Value); //this even supposes index Anyway, just so that people have an overall idea of what can be done, as I'm sure many have stumbled upon this annoyance.

Came up with a quick and dirty fix to this. Just used Hope this helps.

Mmmh... sorry but I don't see how this is answer to the original question. – avernet Nov 2 '09 at 22:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions