Lukas Oppermann
Lukas Oppermann

Reputation: 2938

jQuery .attr retrieving custom attribute returns undefined

i have the following problem using jquery.

I have sth like this

<div id="yxz" value="1">
  <span class="delete"></span>
</div>

now I have this fn but it only returns "undefined", it does however return the id, or class if I ask for this.

$(".delete").click(function(){
  alert($(this).parent("div").attr("value"));
});

I used to get this value with the same attr stuff. Does this have sth to do with me using the jquery 1.6.1 now instead of 1.5.2.

Thanks for your help.

Upvotes: 8

Views: 13368

Answers (5)

metaforce
metaforce

Reputation: 1377

if using the latest jQuery, try with:

$.prop(propertyName);

Upvotes: 0

Melvin
Melvin

Reputation: 547

I can confirm this code snippet works perfectly in Chrome 11, Firefox 4 and IE 9 using the jQuery Git version.

EDIT: For the ones advising prop(), check the docs. Prop() is used for boolean attributes like: checked, disabled etc.

Upvotes: 5

James Allardice
James Allardice

Reputation: 165951

Your code appears to work fine in this fiddle, using jQuery 1.6

Upvotes: 0

Markus Hedlund
Markus Hedlund

Reputation: 24244

Yes, since jQuery 1.6 I think you need to use .prop() instead of attr().

Upvotes: 0

Tim B&#252;the
Tim B&#252;the

Reputation: 63734

To retrieve the value of elements, use val(). Since divs don't have values, you should use data() to set and get data.

Upvotes: 3

Related Questions