Solved

function who clicked

Posted on 2013-10-31
5
226 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

828 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