Solved

function who clicked

Posted on 2013-10-31
5
220 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
Comment Utility
...onclick(function(){...
id=$(this).prop("id")
...
0
 
LVL 42

Accepted Solution

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

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

Expert Comment

by:Gary
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

772 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

10 Experts available now in Live!

Get 1:1 Help Now