Results 1 to 4 of 4

Thread: simulate button click

  1. #1
    Join Date
    Dec 2007
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default simulate button click

    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..

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    For something simple like an alert:

    Code:
    <!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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    SteeleR (03-06-2008)

  4. #3
    Join Date
    Dec 2007
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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 ?

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •