2

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? 
});

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

woa, the answer was in my question :D
1

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

Comments

-1

You can use the js eval function.

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

the eval will execute the string script..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.