Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JQuery select by a <label>'s text or enumerate its attributes?

I'm attempting:

$('label[text="someValue"])

but am getting an empty set returned, most probably since text isn't an attribute.

Is it possible to select by the element's text or inner html?

Edit: :contains("someValue) is not strict enough, since it will return any matches of someValue as a substring.

Is there a way to enumerate all the element's attributes to investigate/interrogate them during debugging/execution?

like image 254
StuperUser Avatar asked Jan 26 '11 18:01

StuperUser


People also ask

Does jQuery support selection based on attributes values?

jQuery [attribute|=value] Selector The [attribute|=value] selector selects each element with a specified attribute, with a value equal to a specified string (like "en") or starting with that string followed by a hyphen (like "en-us"). Tip: This selector is often used to handle language attributes.

How to select jQuery elements?

jQuery uses CSS-style selectors to select parts, or elements, of an HTML page. It then lets you do something with the elements using jQuery methods, or functions. To use one of these selectors, type a dollar sign and parentheses after it: $() .

How to select type using jQuery?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

Which is the correct jQuery selector statement to select all div elements?

Which is the correct jQuery selector statement to select all <div> elements? Explanation: The statement $("div") is the correct syntax to select all <div> elements.


2 Answers

You can select them all and then filter down to a smaller subset by using filter:

$('label').filter(function () {
    return $(this).text() === "someValue";
});
like image 165
nickf Avatar answered Sep 23 '22 08:09

nickf


Tested this:

    $.each($("#form label"), function() {
        var nodes = this.attributes;
        for(var i=0; i<nodes.length; i++)
        {
          alert(nodes[i].nodeName);
         alert(nodes[i].nodeValue);
        }
    });
like image 44
Calvin Froedge Avatar answered Sep 24 '22 08:09

Calvin Froedge