James Huckabone
James Huckabone

Reputation: 625

jQuery - Can't select "label" element with "for" attribute

I'm trying to select the text of the "label" element amongst a large document of similar elements differentiated by the value of the "for" attribute.

If trying to select it like so $("label[for=cu1]").text()

This is not working, nor is selecting the root element.

Here is the example node that contains the label w/ text I'm trying to select. Please keep in mind that there are many other similar nodes, with only the "for" attribute differing (and the id of the input element, if that helps) ...

<div class="verticalfield">
  <label class="verticalfield" for="cu1">Custom 1</label>
  <input id="cu1" class="inputtext long" type="text" value="" maxlength="255" name="cu1">
</div>

Upvotes: 4

Views: 19765

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

I'm going to venture a guess and say that you are trying to use $("label[for=cu1]").text() before that label has been loaded in the DOM. Try something like this:

$(function () {
   //other important stuff

   var text = $("label[for=cu1]").text();
   //etc.
});

Upvotes: 20

Related Questions