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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:05:44+00:00 2026-06-12T17:05:44+00:00

I would like to add a basic click handler to an element using ID.

  • 0

I would like to add a basic click handler to an element using ID. Normally I would use jQuery, but I have forgotten how to do it in vanilla js.

What is the best way to do this, without using jQuery:

$("#myBasic").click(function(){
  alert(“testString”);
});
  • 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-12T17:05:45+00:00Added an answer on June 12, 2026 at 5:05 pm

    Very basic way is to just assign a function to the on[event] property of the element

    document.getElementById("myBasic").onclick = function() { 
                                                     alert("testString");
                                                 };
    

    This has very broad browswer support, and is useful for most basic cases


    There are other event handling features as well, like .addEventListener(evt, fn, capture), but IE only started supporting it in IE9. Before that, you’d need to use attachEvent(onevt, fn), which is similar, but not identical.

    The main trouble with .attachEvent() is that it doesn’t give you an automatic reference to the element that bound the handler via this. But there are workarounds for that.


    To create a fairly browser compatible solution, you could do something like this:

    function addListener(elem, type, fn) {
        if (elem.addEventListener) {
            elem.addEventListener(type, fn, false);
    
        } else if (elem.attachEvent) {
            elem.attachEvent("on" + type, function() {
                                              return fn.call(elem, window.event);
                                           });
        } else {
            elem["on" + type] = fn;
        }
    }
    

    And you’d call it like this:

    var el = document.getElementById("myBasic");
    
    addListener(el, "click", function(e) { alert(this.id); });
    

    This should cover most situations, though I’d be a little concerned about memory leaks for the .attachEvent version.


    All this only deals with binding the handler. To obtain consistent cross-browser behavior, you’d likely need to have a fix ready for the event object.

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

Sidebar

Related Questions

I have the jQuery UI sorting functionality working fine, but I would also like
I have a smallish Yesod application (using the scaffold). I'd like to add basic
I have little experience with Visual Basic. I would like to add some methods
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a hash into an array using Ruby version 1.8.7:
I am trying to do a basic div layout and would like to use
I would like to add basic documentation content to the front page of PYPI
I would like to add some basic html page to my homepage that I
I would like to add items to a dropdownlist progmatically. I have an empty

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.