Solved

function who clicked

Posted on 2013-10-31
5
222 Views
Last Modified: 2013-11-01
I have a function set up to run onclick of multiple items.
How can I figure out what was the id of the object that caused the function to run?
0
Comment
Question by:rivkamak
  • 3
  • 2
5 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39615447
...onclick(function(){...
id=$(this).prop("id")
...
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39615626
This may be picky, but isn't ID an attribute:

id = $(this).attr('id');
0
 
LVL 58

Expert Comment

by:Gary
ID: 39615662
Both work but .prop() gives you the value after any changes (I know it's unlikely that you would be changing an id but...) whilst attr() will only give you the value as it existed at page load regardless of any changes to it.
So I prefer to use prop()
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39615683
Gary - that's not true. prop() and attr() will give you the correct value, even if you change the ID. The ID is an attribute:

From W3c:

http://www.w3.org/TR/SVGTiny12/attributeTable.html

From jQuery Docs:


Summary of Preferred Usage

The .prop() method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr() method.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39615732
attr() will not always give you a dynamic value (tho you are right about the id et al)
Since 1.6.1 using attr() is basically defunct since all it does is call .prop() - so you may as well just use it to start with.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now