开发者

JQuery select text next to an input checkbox?

开发者 https://www.devze.com 2022-12-25 05:14 出处:网络
开发者_JS百科Using JQuery, is there a simple way to select the text immediately after a checkbox?
开发者_JS百科

Using JQuery, is there a simple way to select the text immediately after a checkbox?

<li>bleh..</li>
<li>
    <input type="checkbox" id="cbx1" value="10" />&nbsp;Very important text.
</li>
<li>bleh..</li>

I want to use jquery to select that "Very important text." minus &nbsp;


Better solution might be to wrap the text in a label element:

<li>
    <input type="checkbox" id="cbx1" value="10" />
    <label for="cbx1">Very important text.</label>
</li>

You can then get the text like so:

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

This also improves the semantics of your document.


To do what you said:

var text = $('#cbx1').parent().text();

You might need to trim it though, not sure about the &nbsp;:

var text = $.trim($('#cbx1').parent().text());
0

精彩评论

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

关注公众号