开发者

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();

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜