Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:11:48+00:00 2026-06-01T14:11:48+00:00

I have a page with a few links that are all like this: <a

  • 0

I have a page with a few links that are all like this:

<a href="/" class="answer-item" rel="0">10</a>

I would like to use the click() function to simulate a user’s click on one of them, but it doesn’t seem to work in my tests.

//Evaluate a mathematical expression from another part of the page
var numberAnswer = eval(document.getElementById("question-title").getElementsByTagName("b")[0].innerHTML);

//Builds an array with the links that may match the expression
var choices = document.getElementsByClassName('answer-item');

//Iterates through array to find a match then clicks it
for(var i in choices){
    if(choices[i].innerHTML == numberAnswer){
        choices[i].click();
        break;
    }
}

I’m sure that choices[i] is the correct element.

Firefox does nothing, Opera does nothing, and click() is not available in Chrome (I think).

Also, I have tried to use dispatchEvent() in this form:

var evt = document.createEvent('MouseEvents');
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
choices[i].dispatchEvent(evt);

This apparently returned true in Firefox and Chrome but did not change anything.

The most bothersome part is that a link with only the href attribute works fine with .click().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:11:50+00:00Added an answer on June 1, 2026 at 2:11 pm

    EDIT

    Per discussion in the comment area, it seems that the behavior used in this answer is non-standard, or at least not consistent across user-agents. I am researching this issue further; if you use the information in this answer, please carefully check your code in all browsers to ensure it works as expected.


    EDIT 2

    Per the comment from the OP, there is a “just make it happen” approach that would work here. It has some downsides, namely that your bound events cannot call preventDefault — this method will not respect it. You could build some kind of event wrapper that might be able to deal with this… anyway, here’s the code and fiddle:

    HTML:

    <br><br>
    <!-- I am totally misusing this API! -->
    <a href="http://jsfiddle.net/echo/js/?delay=0&js=The link was followed;" id="progra_link">Some link with an event that uses preventDefault()</a>
    <br><br>
    <button id="doit_btn">Programmatically click the link</button>
    

    Script:

    function do_program_click () {
        var lnk = document.getElementById('progra_link');
        var loc = lnk.href;
        if (!loc)
            return false;            
        // call this to fire events
        lnk.click();
    
        // then follow the link
        document.location = loc;
        return;
    };
    function addEvent(element, evnt, funct){
      if (element.attachEvent)
       return element.attachEvent('on'+evnt, funct);
      else
       return element.addEventListener(evnt, funct, false);
    }
    
    // bind an event to the link to test force event trigger
    addEvent(
        document.getElementById('progra_link'),
        'click',
        function (e) {
            e.preventDefault();
            alert('Testing element click event, default action should have been stopped');
            return false;
        }
    );
    // bind event to the leeroy button
    addEvent(
        document.getElementById('doit_btn'),
        'click',
        do_program_click
    );
    

    jsFiddle: http://jsfiddle.net/CHMLh/


    Original answer

    Your sample code is incomplete. If I take just the basics, it works correctly:

    <script>
       var lnk = document.getElementById('test');
       lnk.click();
    </script>
    <a id="test" href="/" class="answer-item" rel="0">test link</a>
    

    jsFiddle: http://jsfiddle.net/cgejS/

    I would re-evaluate your assumptions that you’re dealing with the correct dom element.

    On an unrelated note, this:

    var numberAnswer = eval(document.getElementById("question-title").getElementsByTagName("b")[0].innerHTML);
    

    … what? eval is evil — if you’re ever using it for any reason, question whether you have the right approach. Are you trying to get an integer out of a string? See parseInt (docs), the right tool for this job:

    // this line is still failure-prone...
    var ele = document.getElementById("question-title").getElementsByTagName("b")[0];
    var numberAnswer = 0;
    if (ele)
       numberAnswer = parseInt(ele.innerHTML);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using markdown a lot and would like to have a few pages that
I have a few tabs on a page that have this markup <div id=holiday-details-nav>
As you all know, it's extremely common to have code like this: <a href='#'
I have a master page which containing few CSS links and java scripts. <html
I have a few links across the page with the purpose of going to
I have a page that takes a few minutes to run. When I set
I have a page that I want to update non stop, every few seconds.
I have a module that is being used to create only a few page
I'm using Django 1.1, and I have this template, a base template, that all
I have a few links on my site that will need to show a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.