javascript - Jquery - extracting and modifying text from an element -


i have elements in document like:

<div class="checkbox-inline"><label><input id="mylabel" value="false" type="checkbox">mytext</label></div> 

when try text using:

$("#mylabel").text(); 

i error text() not defined on object. ids unique, object in 0: position in $("#mylabel") both of these return empty string text:

$("#mylabel").first().text(); $("#mylabel")[0].text(); 

how can text mytext out of these elements? , how can programmatically modify it?

i realize problem different thought was. please see: modifying text of label contains input (checkbox) follow-up question. thanks!

i'm assuming you're trying access text of label element, in case "mytext"? reason isn't working because id on input element, whcih doesn't contain text

<input id="mylabel" value="false" type="checkbox"> 

that's entirety of input element.

as others have stated, can value of element using

$("#mylabel").val() 

which give "false"

however, if need text "mytext" , don't want change markup can use this

$("#mylabel").parent().text() 

which gets element mylabel id, finds it's parent element (in case label element) , gets text element.

now know that, might realise it's easier put id on label!


Comments