Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery attributes from multiple elements

Tags:

jquery

ajax

php

I am trying ot figure how to use jQuery to get the value of html attribute of many elements. My webpage updates dynamically using ajax. I have an attribute called number for in element in the part that is updated. I want to use the attribute value from each element so that i can use that data as parameters to a php file link. I have come across jquery's .attr() function, but it only seems to take the attribute value of the first element if finds. But what I want to do is get the attribute value for each element so that when I click on that element its corresponding attribute value is sent as parameters to the php file.

Thanks

like image 605
Shanon Avatar asked Nov 30 '10 11:11

Shanon


2 Answers

you can combine attr() with .each() method.

e.g.

$("div").each(function(){
     $(this).attr("number");
});
like image 165
Chinmayee G Avatar answered Oct 16 '22 12:10

Chinmayee G


Disclaimer: This most likely does not respond to the OP question (after re-reading), but will stay for some time in case it fill some need of the OP.


Use the .map() method

var numbers = $('[number]').map(function(){
  return $(this).attr('number');
});

this will create an array filled with the number attribute of all the elements that have one.

like image 39
Gabriele Petrioli Avatar answered Oct 16 '22 14:10

Gabriele Petrioli