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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:36:51+00:00 2026-06-10T20:36:51+00:00

There are many ways by which we can attach an event on an HTML

  • 0

There are many ways by which we can attach an event on an HTML element.
The first way is: HTML attribute

<div id="foo" onclick="print2()> My event is attached as HTML attribute</div>

The second way is using some library (say jQuery)

<div id="bar"> My event is attached using jQuery </div>
<script>
    $("#bar").click(function() {
        alert("Hi this is Bar");
    }
</script>

I earlier thought that jQuery might be internally converting the event to corresponding HTML attribute but this does not happen. Check this http://jsfiddle.net/blunderboy/wp4RU/3/

I am logging all the attributes of foo and bar and see bar does not have onclick attribute.
Please explain.

  • 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-10T20:36:53+00:00Added an answer on June 10, 2026 at 8:36 pm

    There is nothing called HTML Event! The two types of events you have described are, inline events and unobtrusive events, and both are javascript events.

    Inline Events

    When you declare javascript code on the elements itself, then it becomes an inline event. You have a few common events (as attributes to HTML Elements) like onclick, onkeydown, onkeypress, onkeyup, and all of them start with on. One such example is:

    <a href="#" onclick="alert('You clicked me!')">Click Me!</a>
    

    Unobtrusive Events

    We need to assign something to be performed when the event is triggered. The = symbol is always used in JavaScript to assign the value on the right to the method or property on the left.

    The window is not the only object we can attach events to. We can attach events to any object within the web page provided that we have a way of uniquely identifying that object. One way of identifying an object is by giving it an ID and referencing it by document.getElementById("id_of_the_element").

    Lets take the same example.

    <a href="#" id="clickme">Click Me!</a>
    

    Instead of the onclick attribute, I have an ID in the same place, which uniquely identifies the HTML element <a>. Now I can get the ID inside javascript this way:

    document.getElementById('clickme');
    

    For this, I can attach an event handler, which doesn’t differ much from the way we use the attributes. It just doesn’t have the on in the front. In our previous example, we used onclick, but now we are just going to use click.

    document.getElementById('clickme').click = functionName;
    

    Here, the functionName refers to any javascript’s function name, or an anonymous function. So, for the alert, if we create a function named alertme(), we can define this way:

    function alertme()
    {
        alert('You clicked me!');
    }
    

    Now to attach the function to the element can be done this way:

    document.getElementById('clickme').click = alertme;
    

    Still feeling lazy, you can do it using the anonymous function way, which takes no name:

    document.getElementById('clickme').click = function () {
        alert('You clicked me!');
    }
    

    Hope you understood. 🙂 Let me know for further clarification.

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

Sidebar

Related Questions

There are many ways functions can be defined in Scala, which leads to confusion
There are many ways of converting a String to an Integer object. Which is
I can see there are many ways to use jQuery safely in the javascript
When validating a document in the ItemAdding event there are many ways to display
There are many ways we can deal with CSS browser compatibility issue. Such as
There seem to many ways to skin this particular cat - but which is
There are so many different ways to include JavaScript in a html page. I
There're many ways to write an HTTP-status header: HTTP/1.1 404 Not Found Status: 404
There are many ways to Title Case in java . But how do you
As SpriteMethodTest says there are many ways for drawing sprites. Firstly, I tried canvas,

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.