Shaoz Shaoz - 1 year ago 111
Javascript Question

What's the equivalent of getElementsByTagName in jquery

What's the equivalent of

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

Answer Source


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

    // do something by directly manipulating the wrapped DOM element = "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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download