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

  • Home
  • SEARCH
  • 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 8924401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:24:11+00:00 2026-06-15T07:24:11+00:00

I am trying to set the onclick event using javascript. The following code works:

  • 0

I am trying to set the onclick event using javascript. The following code works:

var link = document.createElement('a');
link.setAttribute('href', "#");
link.setAttribute('onclick', "alert('click')");

I then use appendChild to add link to the rest of the document.

But I obviously would like a more complicated callback than alert, so I tried this:

link.onclick = function() {alert('clicked');};

and this:

link.onclick = (function() {alert('clicked');});

But that does nothing. When I click the link, nothing happens. I have testing using chrome and browsing the DOM object shows me for that element that the onclick attribute is NULL.

Why am I not able to pass a function into onclick?

EDIT:

I tried using addEventListener as suggested below with the same results. The DOM for the link shows onclick as null.

My problem may be that the DOM for this element might not have been fully built yet. At this point the page has been loaded and the user clicks a button. The button executes javascript that builds up a new div that it appends to the page by calling document.body.appendChild. This link is a member of the new div. If this is my problem, how do I work around it?

  • 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-15T07:24:15+00:00Added an answer on June 15, 2026 at 7:24 am

    I have been unable to reproduce the problem. Contrary to the OP’s findings, the line below works fine on the latest versions of IE, FF, Opera, Chrome and Safari.

    link.onclick = function() {alert('clicked');};
    

    You can visit this jsFiddle to test on your own browser:

    http://jsfiddle.net/6MjgB/7/

    Assuning we have this in the html page:

    <div id="x"></div>
    

    The following code works fine on the browsers I have tried it with:

    var link = document.createElement('a');
    link.appendChild(document.createTextNode("Hi"));
    link.setAttribute('href', "#");
    link.onclick= function() {link.appendChild(document.createTextNode("Clicked"));}
    
    document.getElementById("x").appendChild(link);
    

    If there is a browser compatibility issue, using jQuery should solve it and make code much much more concise:

    var $link = $("<a>").html("Hi").attr("href","#").click(function (){$link.html("Clicked")})
    
    $("#x").html($link)
    

    If brevity is not a strong enough argument for using jQuery, browser compatibility should be … and vise versa 🙂

    NOTE: I am not using alert() in the code because jsFiddle does not seem to like it 🙁

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set an onclick event for thumbnails that are dynamically populated
I am trying to attach an onclick event to a set of links with
I am currently trying to update a textbox using an 'onclick' event inside a
I am trying to add a dynamic onclick event using rich:findComponent as: <font class=topFirstTablehdCategory2
When trying to set DoMatchParen I get the following error: E492: Not an editor
I have the following code in jQuery for a set of radio buttons which
Hi and thanks for reading. I've been trying to avoid using HTML onclick=__ references
I am trying to find way to be able to fire an onclick event
I have the following bit of code. I'm trying to dynamically add drop-down options
I'm trying to use javascript to code up a dynamic form, where elements are

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.