Member Avatar for riahc3

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'); 
Member Avatar for riahc3

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

That does not work.

Member Avatar for Taywin

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?

Member Avatar for EvolutionFallen

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

Member Avatar for AleMonteiro

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

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

Member Avatar for EvolutionFallen

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()?

Member Avatar for EvolutionFallen

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

Member Avatar for AleMonteiro

EvolutionFallen,

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

Member Avatar for EvolutionFallen

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.