www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to create a timer to run an HTML click 'class'

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    1

    Angry How to create a timer to run an HTML click 'class'

    So I am very new to JS and I have been messing around with a template file. I have been lucky so far in searching and finding most of my issues, but I don't think I know exactly what to search for in this issue.

    Here it is:

    There is a picture, that when clicked, runs part of a JS file that changes the background on the website.

    Code:
    <a href="#" class="next"><img src="images/marker_right.png" alt="" class="img"><img src="images/marker_right_active.png" alt="" class="img_act"></a>
    the class 'next' is what changes the image. How can I go about creating a timer in my HTML page that will automatically "click" this every 20 seconds. Keeping the actual physical click functionality of the images alive, as well.

    Ultimately, I want the background to change by itself if the user does not click on the image, every 20 seconds.

    Any ideas?!

  2. #2
    Join Date
    Mar 2012
    Location
    Saint-Petersburg, Russia
    Posts
    97
    Any ideas?!
    I do not think it is a great problem, though I am not sure I understand it correctly.

    To perform a software click you need to find an element with JS (for which I recommend adding of "id=..." attribute to link element) and call .click() on it. If you just want to change its class, you can do it without click either.

    Setting up timer for clicking looks like:
    Code:
    setInterval(myClick, 20000);
    
    function myClick() {
        document.getElementById("myLink").click();
    } // myClick
    Remember to add id "myLink" to the link (or something like this) - id-s are usually used especially for simplifying searching for html element inside a script (though there are other options).

  3. #3
    Join Date
    Feb 2012
    Posts
    218
    If you want to change background every 20 seconds, you don't need click function.
    Code:
    function changeBackground(){
       document.getElementByTagName('body')[0].style.background = 'url("...")';
    }
    window.setInterval('changeBackground()', 20000)

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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