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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:20:36+00:00 2026-06-12T01:20:36+00:00

I have a huge jQuery application, and I’m using the below two methods for

  • 0

I have a huge jQuery application, and I’m using the below two methods for click events.

First method

HTML

<div id="myDiv">Some Content</div>

jQuery

$('#myDiv').click(function(){
    //Some code
});

Second method

HTML

<div id="myDiv" onClick="divFunction()">Some Content</div>

JavaScript function call

function divFunction(){
    //Some code
}

I use either the first or second method in my application. Which one is better? Better for performance? And standard?

  • 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-12T01:20:38+00:00Added an answer on June 12, 2026 at 1:20 am

    Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

    Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

    var myEl = document.getElementById('myelement');
    
    myEl.addEventListener('click', function() {
        alert('Hello world');
    }, false);
    
    myEl.addEventListener('click', function() {
        alert('Hello world again!!!');
    }, false);
    

    http://jsfiddle.net/aj55x/1/

    Why use addEventListener? (From MDN)

    addEventListener is the way to register an event listener as specified
    in W3C DOM. Its benefits are as follows:

    • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that
      need to work well even if other libraries/extensions are used.
    • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
    • It works on any DOM element, not just HTML elements.

    More about Modern event registration -> http://www.quirksmode.org/js/events_advanced.html

    Other methods such as setting the HTML attributes, example:

    <button onclick="alert('Hello world!')">
    

    Or DOM element properties, example:

    myEl.onclick = function(event){alert('Hello world');}; 
    

    are old and they can be over written easily.

    HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

    The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

    More about Traditional event handling -> http://www.quirksmode.org/js/events_tradmod.html

    MDN Reference: https://developer.mozilla.org/en-US/docs/DOM/event

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

Sidebar

Related Questions

I made an application using jQuery mobile, but now I have a huge problem.
I have made a simple chat script using jquery and AJAX but the huge
I have a div that has two buttons in it that has a jQuery
I'm using Asp.NET MVC and have a huge dependency on JQuery throughout my entire
I'm new to JQuery and don't have a huge background in Javascript, either -
I have a huge project that I want to move from JQuery 1.4.x to
I have a simple accordion setup, with just two sections. The top, first section
I have a bunch of HTML that is generated by a daemon using C,
I have a web application written completely in the usual html/css/javascript and some additional
I have been using the DataTables plugin for jQuery (http://www.datatables.net) to search, sort and

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.