View Full Version : how do i click a button in a page using javascript?

10-27-2004, 08:20 PM
how do i click a button in a page using javascript?

10-27-2004, 08:36 PM
Heh, hey. welcome to the forum.

you might need to be a little more specific... can you give an example of what you mean, a url to an example, or a more descriptive elaboration?

10-27-2004, 08:40 PM
there is a button in the page
<p><input type="submit" name="button" value="button"></p>
how do i click the button using JAVESCRIPT and not by the mouse when the page is loaded?

10-27-2004, 10:09 PM
so you want this button to be activated when the page is loaded?

i know its possible... i dont offhand know how. it would use either

<body onload="something">

maybe something like

function clickOnLoad()
document.formname.button_name.value = "true";

<body onload="clickOnLoad()">

formname is the name of your form
button_name is the name of the button

I dont know about the .value = true; part... someone will have to check that for me

Roy Sinclair
10-27-2004, 10:11 PM
You want a form to submit all by itself when the page containing the form is loaded? The following should work as long as you've only got one form on the page.

<body onload="document.forms[0].submit();">

10-27-2004, 11:38 PM
and if not would you just add the formname in instead of the 0?


<body onload="document.forms[formname].submit();">

10-27-2004, 11:39 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<input id="btn" type="button" value="press me">
<script type="text/javascript">
document.getElementById("btn").onclick = document.getElementById("btn").disabled = true;

10-28-2004, 05:22 AM
errmm... Hemebond, what exactly does that do to help this issue at hand...? from what i get that disables the button once its clicked...


10-28-2004, 06:08 AM
The button is disabled because it has been clicked. I've just shown how to click a button using Javascript.

If he just want's to submit a form when the page loads, that's extremely simple; but for anyone looking for a way to click a button using Javascript, there's the solution.

10-28-2004, 06:17 AM
isn't there an obj.click() ? which simulates clicking a button? although it's not speficied in any standard, I am pretty I've seen it in the JS reference for IE and Mozilla/Firefox.

edit: link, http://www.mozilla.org/docs/dom/domref/dom_el_ref34.html#1028373

10-28-2004, 07:32 AM
Button.click() is not only not a part of any standard (as msdn would say), it's poorly supported.

Roy S. answered this properly. A button is a UI widget - it allows a user to launch script, or whatever. You're not a user (well, not in this case, anyway). Program it! :thumbsup:

10-28-2004, 02:09 PM
good point Heme, i hadnt seen it that way. :p