Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

function who clicked

Posted on 2013-10-31
5
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 58

Assisted Solution

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

Accepted Solution

by:
Chris Stanyon earned 1000 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 44

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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

715 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