How to get plain text next to an HTML element using jQuery?

<span>
    <label for="305">
        <input type="checkbox" name="305" style="vertical-align: middle;" value="305" id="305"> Farming General
    </label>
</span>

How to get the text " Farming General " using jQuery. Please do not ask me to change the structure of HTML

+5
source share
2 answers

You can capture it with an identifier selector by going to .parent()and using .text():

​$("#305").parent().text()
//or:
$("label[for='305']").text();

Although identifiers starting with a number are not valid until HTML5, just keep in mind :)


You can also get it without jQuery, for example:

document.getElementById("305").nextSibling.nodeValue

Although you can crop it with $.trim()for example:

$.trim(document.getElementById("305").nextSibling.nodeValue)
+11
source

This is the simplest thing I think.

$("label[for='305']").text();

0
source

All Articles