Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery select text next to an input checkbox?

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;

like image 418
Rosdi Kasim Avatar asked Mar 30 '10 02:03

Rosdi Kasim


1 Answers

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.

like image 61
harto Avatar answered Oct 14 '22 04:10

harto