1.11M Members

How do I automatically click a button using Javascript?

 
0
 

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?

 
0
 

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

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

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

That does not work.

 
1
 

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?

 
0
 

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

 
0
 

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

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

 
0
 

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

 
0
 

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

 
0
 

EvolutionFallen,

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

 
0
 

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

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article