<span>
<label for="305">
<input type="checkbox" name="305" style="vertical-align: middle;" value="305" id="305"> Farming General
</label>
</span>
How do I get text "Farming General" using jQuery. Please do not ask me to change HTML structure
Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.
jQuery html() MethodThe html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element.
Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.
You can grab it using an ID selector, going to the .parent()
and using .text()
like this:
$("#305").parent().text()
//or:
$("label[for='305']").text();
Though, IDs starting with a number aren't valid until HTML5, just be aware :)
You can also get it without jQuery, like this:
document.getElementById("305").nextSibling.nodeValue
Though you may want to trim it down with $.trim()
, like this:
$.trim(document.getElementById("305").nextSibling.nodeValue)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With