Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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>
like image 792
James Huckabone Avatar asked Nov 30 '22 04:11

James Huckabone


1 Answers

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.
});
like image 84
Explosion Pills Avatar answered Dec 04 '22 10:12

Explosion Pills