How do I Get Plain Text Beside 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 do I get text "Farming General" using jQuery. Please do not ask me to change HTML s开发者_StackOverflow社区tructure
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)
This is the simplest I think.
$("label[for='305']").text();
精彩评论