2
jQuery(function () {
    $('#square a').click(function(e) {  
    // How is the e object structured
    }); 
})

I have a div with an id of square and links in that. When the links are clicked they trigger a game loop function. I need to know which id the click is coming from. There are five links within the square and each one has an id. Is there a better way to track this. Or should I have a function for each link with its own id but this doesn't seem DRY to me.

thenengah
  • 42,557
  • 33
  • 113
  • 157

6 Answers6

5

Inside an event handler you can refer to the DOM element the event was triggered on with this:

$('#square a').click(function(e) {  
    alert(this.id);
});

So jQuery makes it very convient to access the element.

If you have to execute different code based on the ID, I suggest to create some kind of map:

var actions = {
    action1: function() {

    },
    action2: function() {

    },
    ...
}

and then just execute the function from the map:

$('#square a').click(function(e) {  
    actions[this.id]();
});

Just make sure you give the elements meaningful IDs.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Not only do you need to give them meaningful IDs, you should try to keep them unique, as that is the rule. Additionally, @FelixKling good point on the closure, makes it even easier to maintain (if it does look a little funky) – jcolebrand Dec 14 '10 at 00:11
  • Must be a mind reader. I knew how to get things working after this but your code method is much better ;) – thenengah Dec 14 '10 at 00:12
3

The e in your code is an event object. The docs for that are here. Within an event handler, this refers to the DOM element that you have hooked the handler to (not wrapped in a jQuery instance). So for instance:

jQuery(function ($) {
    $('#square a').click(function(e) {  
        alert(this.href);
    }); 
});

...would show the href of the link that was clicked.

Since click events bubble up the DOM, and they also have a default behavior on a elements (following the link), you may particularly be interested in event.stopPropagation (stop the event bubbling any further up the DOM) and event.preventDefault (prevent the default action). You can get the effect of calling both of those if you simply return false out of your event handler, but sometimes you only want to do one or the other.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

Something like this?

<div id='square'>
  <a id='link1'>Go forward!</a><br />
  <a id='link2'>Look left</a><br />
  <a id='link3'>Stop!</a><br />
  <a id='link4'>Look right</a><br />
  <a id='link5'>Go back!!!</a>
</div>

$('#square > a').live('click',function(event){
  var whichLink = $(this).attr('id');

  if ( whichLink == 'link1' ) GoForward();
  if ( whichLink == 'link2' ) LookLeft();
  if ( whichLink == 'link3' ) Stop();
  if ( whichLink == 'link4' ) CantTouchThis();
  if ( whichLink == 'link5' ) Hammertime();
});
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
3

A good way to quickly check things like this is with the console of Firefox (Firebug), Chrome and Safari (I believe that Opera has something similar as well.):

If you run the following code on any web page with jQuery on it via the console of Firebug:

jQuery("body").click(function(e) { console.dir(e); });

You will see the following: Firebug output from the dir command

T. J. has provided another excellent source as well (the jQuery API docs.)

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • *"The best way to check things like this is with Firebug"* Enh, that will show you not only fairly standard things and the things that jQuery ensures are standardized, but a *lot* of Gecko-specific stuff as well. Firebug is very useful, but does inevitably show all the extensions that Gecko has on top of the DOM. – T.J. Crowder Dec 14 '10 at 00:10
  • @T.J. -- an excellent point - I'll update the question to be less forceful. – Sean Vieira Dec 14 '10 at 00:12
  • @Sam -- I believe that Safari has a console with the same method: http://stackoverflow.com/questions/55633/where-is-the-console-api-for-webkit-safari – Sean Vieira Dec 14 '10 at 00:13
  • 1
    Yup. @Sam, in Preferences | Advanced, tick the "Show Develop menu in menu bar" and close the preferences. Then you'll find on gear menu or whatever, there's a "Develop" menu with *lots* of fun stuff to play with. :-) – T.J. Crowder Dec 14 '10 at 00:18
2
$('#square a').click(function(e) {  
    alert($(this).attr("id"));
}); 

I hope this is what you are looking for

Ives.me
  • 2,359
  • 18
  • 24
2

Another way to get clicked element's id that has not been mentioned is through the event object and answers your question about e parameter:

jQuery(function () {
    $('#square a').click(function(e) {  
       alert(e.target.id);
    }); 
});

The advantage of using e.target.id is that you can obtain id of the child object that was actually clicked on, while this.id will always return id of the element click event handler was bound to.

Also, if you're going to use this there is no reason to declare e:

$(document).ready(function() {
    $('#square a').click(function() {  
       alert(this.id);
    });
});
vls
  • 2,304
  • 15
  • 20