views:

596

answers:

3

Currently I'm using something like:

$('.myclass').click(function(){  
    var msg = $(this).attr('id');  
    alert(msg)
});

And HTML:

< a href="#" class="myclass" id="101">Link</a>

If I need additional parameters how would I read them? Also is the current way I am using the proper way? Originally I was using hidden input fields so it was already a step up. :p

A: 

you can use bind() and send additional data to the event handler as event.data

Russ Cam
A: 

jQuery Events

$('.myclass').bind("click", { Param1: "", Param2: 2 }, function(event){
    alert(event.data.Param2);
});
ChaosPandion
Would that mean I'd have to include this function on the same page? Currently all my js stuff is in a separate file.
Roger
+1  A: 

this may not be the best method, but something i have done is to hyphen delineate the id attribute with other params, like id="101-red-420582" which all three mean different things and can be easily broken down using the split() function. not the best, per se, but i've done it and it works.

Jason
Coincidentally I came up with this exact idea and it was working fine, but like you said not too sure if its the correct way and sometimes the string might get quite large (not sure if there is a limit of the id).
Roger
I use a similar method all the time, but instead of the 'id' I use the 'rel' attribute. I believe either will hold as much information as you need, including HTML.
fudgey