Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool document.getElementById('apDiv1').click() Not working on a AP Div element.

    Hi, here is my script: But it won't work!!!!! Why ?

    <html>
    <head>
    <script type="text/javascript">
    function clickButton()
    {
    document.getElementById('apDiv1').click()
    }
    function alertMsg()
    {
    alert("Button 1 was clicked!")
    }
    </script>
    <style type="text/css">
    #apDiv1 {
    position: absolute;
    width: 200px;
    height: 115px;
    z-index: 1;
    left: 45px;
    top: 222px;
    }
    </style>
    </head>
    <body onLoad="clickButton()">
    <div id="apDiv1" onClick="alertMsg()"><img src="bedroom.png" name="pic" width="150" height="150" id="pic"></div>
    </body>
    </html>

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,607
    Thanks
    6
    Thanked 997 Times in 970 Posts
    What is this? Are you using a JS framework? Because click() is not standard JS. And if you are then why are you mixing things up with inline onload event handlers and document.getElementById rather than using the methods built into the framework?

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Hi, I am trying to make java auto click on a Ap Div element that has a on click event.

    I am using Dreamweaver.

    Thanks and sorry for the confusion!
    Ron

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    [QUOTE=ronwilliams;1238080]Hi, I am trying to make java auto click on a Ap Div element that has a on click event.[/code]

    This is the JavaScript forum - not the Java forum - they are two different languages.

    Quote Originally Posted by ronwilliams View Post
    I am using Dreamweaver.
    What program you use to generate the HTML makes no difference - there is no program to auto generate JavaScript so you have to write it yourself. There are some libraries of JavaScript code some of the JavaScript experts have written that can make writing JavaScript easier once you understand enough javaScript to be able to use them. Some of those libraries implement a click() function.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply!

    I am using safari . Any workaround to get this working under these browsers?
    Thanks

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,883
    Thanks
    56
    Thanked 539 Times in 536 Posts
    if you want the function to run on load, why not just call it directly then as opposed to simulating a click which will then run the function?

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    here is some code to
    illustrate xelawho's
    suggestion ...

    Code:
    <html>
    <head>
    <style type="text/css">
    #apDiv1 
    {
    	position: absolute;
    	width: 200px;
    	height: 115px;
    	z-index: 1;
    	left: 45px;
    	top: 222px;
    }
    </style>
    </head>
    <body>
        <div id="apDiv1" onClick="alertMsg()">
    	<img src="bedroom.png" name="pic" width="150" height="150" id="pic">
        </div>
    <script type="text/javascript">
    
    function alertMsg(){
    	alert("Button 1 was clicked!");
    }
    alertMsg();
    </script>
    </body>
    </html>


  •  

    Posting Permissions

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