0

I need to change the value of the url of the onclick portion of my input tags

<input type="image" onclick="return launchEditor('image1', 'http://images.aviary.com/imagesv5/feather_default.jpg');" />

I have the needed url being dynamically stored in a variable on hover function.

$('ul.photos img').hover(function(){
var imgSrc = $(this).attr("src");

How can I take this variable and plug it into the needed area? Thanks for any help

2
  • 2
    I suggest replacing the onclick attribute with a jQuery event handler. Commented Apr 3, 2012 at 0:58
  • @Tuan why? Event handlers are harder to debug, and usually require a fallback. Commented Dec 9, 2012 at 16:12

3 Answers 3

3

Something like this should work:

<input type="image" onclick="return launchEditor('image1', window.foo);" />

And the hover callback:

$('ul.photos img').hover(function(){
    window.foo = $(this).attr("src");
});

Replace foo by anything you want.

But some more details on what it should do would let us help you better. Even though this should work, it's probably not the cleanest solution.

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

Comments

1

If you have that local variable in your hover handler, you either will have to make it globally available and use it in the onclick function, or you will have to change the onclick function (here: attribute) in the hover handler.

Comments

1

The simplest solution is to add:

<input onmouseover="this.onclick=function(){ [javascript] }"/>

with [javascript] being the code you want executed on click.

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.