开发者

How do I Get Plain Text Beside an HTML Element using jQuery?

开发者 https://www.devze.com 2023-01-09 04:17 出处:网络
<span> <label for=\"305\"> <input type=\"checkbox\" name=\"305\" style=\"vertical-align: middle;\" value=\"305\" id=\"305\"> Farming General
<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();

0

精彩评论

暂无评论...
验证码 换一张
取 消