Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  194] [ 6]  / answers: 1 / hits: 132485  / 14 Years ago, sat, november 6, 2010, 12:00:00

I have this snipped in my page:



$('#category_sorting_form_save').click(function(){
var elements = $(#category_sorting_elements > div);
$.each(elements, function(key, value) {
console.info(key, : ,value);
console.info(cat_id: ,value.attr('cat_id'));
});
});


And when it is executed, I get:



0 : <div class=dragable cat_id=6 value= style=opacity: 1;>    
value.attr is not a function
console.info(cat_id: ,value.attr('cat_id'));


What am I doing wrong here? I am trying to get the value of the div.cat_id element.


More From » jquery

 Answers
140

Contents of that jQuery object are plain DOM elements, which doesn't respond to jQuery methods (e.g. .attr). You need to wrap the value by $() to turn it into a jQuery object to use it.



    console.info(cat_id: , $(value).attr('cat_id'));


or just use the DOM method directly



    console.info(cat_id: , value.getAttribute('cat_id'));

[#95055] Wednesday, November 3, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austenjordang

Total Points: 544
Total Questions: 112
Total Answers: 112

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;