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

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

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

Was it helpful?


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

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

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow