Start New Discussion within our Web Development Community

Hello

Ive created a button dynamically using Javascript and now I want to automatically click on it.

The code I create (actually modify) the button is something similar to:

$j(idtext, window.parent.document).html('I am a button<input type="hidden" id="imagenv" name="imagenv" value="'+imgstr+'"/>');   

Now I want to automatically click on it. Ive tried .live() (I have jQuery) but it doesnt work and I think it doesnt work because the the button I want to click is not in the same document, its in the parent (as you can see the button called idtext is in the "window.parent.document").

So How do I do this?

Since you are using jQuery, you should be able to use:

$j(idtext, window.parent.document).trigger('click');

Since you are using jQuery, you should be able to use:
$j(idtext, window.parent.document).trigger('click');

That does not work.

Why do you want to trigger the click event of a button by simulating the click? Why can't you call the same function that the click event is calling instead?

Assuming that $j(idtext, window.parent.document) is the right selector of your button:

$j(idtext, window.parent.document).click(); should work.

The reason I asked if $(idtext) is a button is because a button can't have HTML content. Therefore calling $(idtext, window.parent.document).html(...) is invalid, would throw an error, and probably kill the script. riahc3, maybe you meant to use .append() instead of .html()?

Also, .click() is just a shortcut for .trigger('click');, so if pritaeas' suggestion didn't work, I can't imagine this will.

EvolutionFallen,

Buttons can have html content.
$("#myButton").html("<b>Button Text</b>"); is a valid command and the text will be in bold.

This article has been dead for over six months. Start a new discussion instead.