Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to get attribute of an element when using the 'click' event

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

like image 693
Roman Avatar asked Sep 22 '11 18:09

Roman


People also ask

How do you get the ID of an element in JavaScript from a clicked?

The buttonPressed() callback function will have a returned event object which has all the data about the HTML element that is clicked on. To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

How can get click event value in jquery?

Try this. $(document). ready(function() { $(this). click(function(event) { alert(event.

How do you pass an element to a onclick function?

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that's called with this . to set the onclick attribute to the a elements to call onClick with this .


1 Answers

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

$('.newItem').click(function () {     alert($(this).attr('setid')); }); 
like image 86
BNL Avatar answered Sep 23 '22 22:09

BNL