www.webdeveloper.com
Results 1 to 7 of 7

Thread: button click

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    button click

    hi, first I'd like to tell you that I'm not a js developer I just need a little help, hope it won't be too complicated to understand for me

    I need to click by code on a button which has no 'ID', 'name' nor 'tag. It has only 'class' and 'tab index'.
    After some googling I found that there is possible to use GetElementbyClassName (which should work on firefox) but I;ve not been able to make it work. Could anybody give me an example of code which can handle this task?

    thank you

  2. #2
    Join Date
    Nov 2010
    Posts
    1,085
    Code:
    document.getElementsByClassName("theclassname")[0].click()
    you might have to change the 0 in [0] depending on how many other elements have that class name, and which order they appear in on the page

    if you want it to work on all browsers (for example, older IE) it would require a little more code. not much, but a little

  3. #3
    Join Date
    Mar 2013
    Posts
    4
    could you give me a simple working example ?

  4. #4
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    447

    javascript

    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function clicked()
    {
    
    alert('button clicked');
    
    }
    
    </script>
    </head>
    <body>
    <form>
    <input type=button  class="class1" onclick="clicked()">
    </form>
    
    <script type="text/javascript">
    document.getElementsByClassName("class1")[0].click();
    </script>
    
    </body>
    </html>
    Note: document.getElementsByClassName is not available in IE
    Chris, Senior Developer,
    Php laravel developers,
    www.chrisranjana.com

  5. #5
    Join Date
    Nov 2010
    Posts
    1,085

  6. #6
    Join Date
    Mar 2013
    Posts
    4
    Thank you for your example, it works great but I still have a small problem. Button I want to click on by this code is defined like this:

    Code:
    <button class="class1">OK</button>
    When I replace your button line with this line , it keeps firing all the time (I know there is no event there but that has the same result e.g. alert window keeps opening.) Why is that? It does not make sense why this ends in some infinite loop.

  7. #7
    Join Date
    Mar 2013
    Posts
    4
    to be more precise button is defined like this:

    Code:
    	<button tabindex="2" class="button class" type="submit">OK</button>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles