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 781471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:14:05+00:00 2026-05-14T20:14:05+00:00

I’m trying to set the onclick attribute of a link generated with jQuery. While

  • 0

I’m trying to set the “onclick” attribute of a link generated with jQuery.

While other attributes work without problem (eg. href, but also other custom attrs), setting “onclick” seems to only work in FireFox. On other browsers the attribute just gets ignored.

Any hint? Is this behaviour correct? And why?

<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
  jQuery(function(){
    var obj = $('<a/>');
    obj.html('click me');
    obj.attr({'href':'#test', 'onclick':'alert("o hai"); return false;'});
    $('body').append(obj);
  });
</script>
</head><body /></html>

EDIT: I know about the .click() jQuery method; but that’s not the point, I just want to set the attribute and can’t figure out why it doesn’t work like any other attr does.

  • 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-05-14T20:14:05+00:00Added an answer on May 14, 2026 at 8:14 pm

    Is this behaviour correct?

    In as much as attr('onsomeevent', ...) has never been the way to add event listeners, has never been supported, and only works on Firefox due to coincidence, yes.

    And why?

    Because IE before version 8 can’t directly get/set HTML attributes. Calling setAttribute() only sets a DOM property of the same name. And the onclick property isn’t a string property so you can’t write a string to it. It only accepts a function value.

    jQuery’s attr() muddies the issue by being called attr but actually writing to DOM properties and not HTML attributes… but using HTML attribute names (like eg. class) rather than DOM property names (like className).

    (Except when it doesn’t, because for some cases it tries to hide the difference by using attributes. Except that still doesn’t work all the time in IE for the reason above. This is not one of jQuery’s proudest moments.)

    Anyhow, in general having JavaScript in a string is a code smell. The best way forward for jQuery and modern JavaScript in general is to use function objects (possibly inline function() { ... } expressions) instead.

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

Sidebar

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.