29

I'm using jQuery to get a click on an 'a' element. I have a list of those links where each one has a class which by him I'm capturing the click.

I would like to know which of the links has been clicked (I have an attribute 'setid' for each link) and also get the point where the mouse has been clicked.

How could this be accomplished?

example of the code:

<a href="#" class="newItem" setid="28">click me</a>

$('.newItem').click(function (e) {
    alert(e.attr('setid'));
});

EDIT: OK, so to get the position I would use e.pageX

2
  • 8
    If you are going to add arbitrary attributes to your HTML, like "setid", you may want to consider adding the "data-" flag as this will pass validation. Essentially setid="28" isn't valid, but data-setid="28" is. Commented Sep 22, 2011 at 18:12
  • Michael - Thanks for the code :) Commented Sep 22, 2011 at 18:22

5 Answers 5

50

To use jQuery methods you have to wrap this with a call to jQuery.

$('.newItem').click(function () {
    alert($(this).attr('setid'));
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, I'm having some problems with the .click(function(e)) { } when using IE9 for an anchor tag. Any idea how to get the attribute without using JQuery .click? example: function doClickFunction() { $(this).attr('class'); } doesn't work when you use plain JS and JQuery $(this)
Ask as new question please.
4

refer to the code given below:

document.getElementById("element_id").addEventListener("click",function(e)
{
 console.log(e.srcElement.getAttribute("data-name"));
},false);

1 Comment

Very nice. Thank you. I was looking for a vanilla way to do this.
2

Like Skylar said in the comment to your question, you can use "data-" in front of your new attribute (which is HTML5 valid).

<a href="#" class="newItem" data-setid="28">click me</a>

Then in jQuery (v1.4.3+) you can get it like:

var setid = $(this).data("foo");

It's even a little nicer than attr() that other people have mentioned. See this for more examples - http://www.broken-links.com/2010/11/18/data-attributes-in-html-and-jquery/

Comments

1

You're on the correct path.

$(function(){

    $('.newItem').click(function () {
        alert( $(this).attr('setid') );
    })
}); 

Comments

0

Your code is almost right to get the attribute. The only thing missing is wrapping this (a DOM element) in the jQuery wrapper so you can use jQuery functions. $(this).attr('setid') should work.

To get the page coordinates, use the pageX and pageY properties of the event object. So:

$('.newItem').click(function (e) { // e is the event object
    alert($(this).attr('setid'));
    alert(e.pageX);
    alert(e.pageY);
});

See the documentation on the Event object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.