views:

25

answers:

3

say there is an element #button with the attribute onmousedown containing some random javascript how could I trigger the js in that element attribute using jQuery?

j("#otherbutton").click(function(){ 
         var script =  j("#button").attr("onmousedown"); //what now? 
});
A: 

You can use the js eval function.

document.write("<br />" + eval("2+2"));

the eval will execute the string script..

ari
+1  A: 

You can use:

eval(script);

The eval() function evaluates and/or executes a string of JavaScript code.

First, eval() determines if the argument is a valid string, then eval() parses the string looking for JavaScript code. If it finds any JavaScript code, it will be executed.

http://www.w3schools.com/jsref/jsref_eval.asp

Ben
+3  A: 

Or j("#button").trigger("mousedown"); which will execute the code in the onmousedown attribute and any event handlers attached to the "mousedown" event.

Strelok
woa, the answer was in my question :D
Moak