PositiveGuy
PositiveGuy

Reputation: 47733

Get text and value from label

Trying to pull the text from a label on the page but it's not working. The id is right that is in the contains.

     var modelid = $("label:contains('singleModelText')").text();

Upvotes: 0

Views: 148

Answers (4)

Nick Q.
Nick Q.

Reputation: 3986

:contains() is a selector that searches an element for a string, returning elements that have that text. The way I interpret your question is you have a document like this:

<label>I want this text <input id="singleModelText" /></label>

If that's not the case feel free to stop here, if that is the case however you want something like this:

$("#singleModelText").parent("label").text();

Upvotes: 0

Clive
Clive

Reputation: 36957

It sounds like you're trying to get the element by ID try:

var modelid = $('label[id*="singleModelText"]').text();

Upvotes: 1

Jason Gennaro
Jason Gennaro

Reputation: 34855

Should be something like

var modelid = $('label[id*="singleModelText"]').text();

See here for more on the "attribute contains selector":

http://api.jquery.com/attribute-contains-selector/

Upvotes: 2

Naftali
Naftali

Reputation: 146302

If $("label:contains('singleModelText')").length == 0 then your selector is not right.

Upvotes: 0

Related Questions