Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  124] [ 7]  / answers: 1 / hits: 94056  / 14 Years ago, thu, december 9, 2010, 12:00:00

What's the equivalent of getElementsByTagName() in jQuery? I just want to create a collection of elements in jQuery so I can iterate through them and do something with each item.



Many thanks!


More From » jquery

 Answers
32
$(tagnamehere)


So:



$(div).each(function() {
// do something exciting with each div
$(this).css(border, 1px solid red);

// do something by directly manipulating the wrapped DOM element
this.style.border = 1px solid red;

// do something only if this particular div has a class of 'pretty'
if($(this).hasClass(pretty)) {
$(this).text(I am the pretty one);
}
});


or just:



// apply some css to all div elements
$(div).css(border, 1px solid red);


Keep in mind that when you use jQuery to select a number of elements, e.g. $(span), any method you invoke on the object will happen on all matched elements. Think of it as 'implicit iteration' - e.g. $(span).hide(); will hide all span elements on the page.



See:




[#94665] Tuesday, December 7, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;