PDA

View Full Version : simulate button click



SteeleR
03-06-2008, 12:35 PM
the idea is as follows:

i need to be able to click one button and trigger another buttons click event i.e:

function trigger(id)
{
document.getElementById(id).click();
}

<input type="button" onclick="trigger('z')" name="first" value="first" />
<input type="button" id="z" onclick="alert('zzzz')" name="second" value="second" />


the final effect should be that when i press the 'first' button.. a "zzzz" alert box will show up (simulating a 'second' button click)... but the click() event is not working.. How can i do this..

jscheuer1
03-06-2008, 12:54 PM
For something simple like an alert:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function trigger(id)
{
document.getElementById(id).onclick();
}
</script>
</head>
<body>
<input type="button" onclick="trigger('z')" name="first" value="first">
<input type="button" id="z" onclick="alert('zzzz')" name="second" value="second">

</body>
</html>

If button z's event is more complex, trigger() may need to be more complex.

SteeleR
03-06-2008, 01:02 PM
how much more.. cause the onclick() really helps execute the onlick event.. but does not simulate button click..

my actual use is to get a file browser and the second input is a type="file":

function trigger(id)
{
document.getElementById(id).onclick();
}

<input type="button" onclick="trigger('z')" name="first" value="first" />
<input type="file" id="z" onclick="alert('zzzz')" name="second" value="" />

but it turned out that this is not enough. the "zzzz" is shown.. but not the file browser as when there is avtual button hit..

any other such function ?

jscheuer1
03-06-2008, 01:49 PM
File inputs are a different sort of input. Because they access the user's hard drive, they have a much higher security threshold than any other input type. I don't believe that you can script them, at least not in the manner you describe.