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?

Can you clarify what is in idtext? Is that a reference to the actual button, or some other element?

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.

True, really depends on if the OP is using <button> or <input type="button">. I forgot about <button> ^.^;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.