Here’s some example code:
<input type="checkbox" id="send-notice" name="send-notice" value="1" /> This is a notice to all users.
<label for="subject">Subject</label>
I want to be able to select the text “This is a notice to all users.” so I can remove it.
I’m not sure how to do so though.
I’ve tried using jQuery('#send-notice').next(), but that selects the <label for="subject"> block.
I also tried wrapping a <div> around the text using jQuery('#send-notice').after() and a closing </div> tag on jQuery("label[for='subject']").before(). But jQuery doesn’t like unopened elements.
Any help here?
You want to operate on textnodes, and jQuery steers clear of textnodes. Since you can’t wrap the text you want to change in a span or something like that, you can, with some effort, identify the textnodes and do some surgery. This is thrown together quickly, but seems to work:
This can certainly be reduced and streamlined, but it seems to work using the information in your question, and should give you a starting point on the approach. It doesn’t rely on matching your string exactly — just that the string will be positioned in some larger container (I have it as
div#mainhere), and it won’t have any non-whitespace siblings. Even if your case is more complex, as long as the text is consistently placed you should be able to identify it within thefilter()function.