Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i am and absolute beginner to jquery and java script. I am trying to add delegate event handlers
This is the task that i am required to do, anchor elements with class .user_link from #user_list
the delegate events that i am using are #user_list for "li a.user_link". I am a bit confused and i thought that perhaps this how it should be done but it is not i guess.


JavaScript
$("#li a.user_link user_list").on("click", "user_list", handleGetUser(event){
		alert($(this).text());
		});


Whats the correct way of doing this task?
thanks anyways guys
Posted
Comments
Sergey Alexandrovich Kryukov 4-Apr-15 21:06pm    
What do you mean by "delegates events". It sounds like you have been distracted by some object-oriented languages/technologies, but even for them, this is not a correct terminology.

In JavaScript, things are very different. There are is no difference between "functions", "delegates" or "delegate instances"... All objects are 1st-class objects, including functions.

Why not just trying it out, your code fragment?

—SA
Sergey Alexandrovich Kryukov 4-Apr-15 21:08pm    
...also, there is no such thing as "java script". Better never use such words. Java is not a scripting language ;-)
JavaScript is totally unrelated to "Java".
—SA

1 solution

There can be different ways to add a handler to an event, or to set the only handler.

First, some background:

This is the simplest way, when you just set a handler:
JavaScript
someElement =
   document.getElementById("someId"); // I'm not going to check up your selectors 
                                      // without seeing your HTML and the specs
someElement.onclick =
   function(event) {/* ... */} // you can use event object
// or
someElement.onclick = function() {/* ... */}

You can add event handler (called event listener) and have more than one handler on the same element (same event instance):
JavaScript
someElement.addEventListener("click", function() {/* do something  */});


See: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener[^].

Now, you might better understand what jQuery does: https://api.jquery.com/on.

That is,
JavaScript
someElement.on( "click", function() {
    // do something
});

In this function, the arguments selector and data are optional, please see the jQuery documentation page on "on" referenced above.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900