Multiple Element jquery Attributes

I am trying to figure out how to use jQuery to get the html attribute value for many elements. My webpage is being updated dynamically using ajax. I have an attribute called the number for the item in the updated part. I want to use the attribute value from each element so that I can use this data as parameters to reference the php file. I came across the jquery.attr () function, but it only seems to accept the attribute value of the first element if it finds one. But what I want to do is get the attribute value for each element so that when I click on this element its corresponding attribute value is sent as parameters to the php file.

thanks

+4
source share
3 answers

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

eg.

 $("div").each(function(){ $(this).attr("number"); }); 
+10
source

Disclaimer: This most likely does not answer the OP question (after re-reading), but will remain for some time if it fills some OP need.


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 elements that have one.

+8
source

Inside the click handler (or any event handler), this will refer to this element, for example:

 $("#content").delegate("a", "click", function() { alert($(this).attr("something")); //alerts "something" for the <a> you clicked }); 

In this case, we use .delegate() because you said: “My web page is updated dynamically with ajax,” so just attach the handler to the parent element, which is not replaced via AJAX, and it will work for all elements that you add below ... in the above example, we bind to all the <a> elements, but just change the selector accordingly.

+3
source

All Articles