i've got this:
<div class="" id="fancy_hover">
<a href="home.html" id="start_writer"></a>
<div>
<p class="hidden" style="display: block;">click here to see this event with all the bells and whistles</p>
</div>
</div>
I need to do an equivalent of .find()
but for the string "click here"
instead of a node, i then need to wrap it in an <a>
tag.
Whats the best approach with jquery?
If you've faced the situation when you need to wrap words in a <div>, you can use the white-space property with the "pre-wrap" value to preserve whitespace by the browser and wrap the text when necessary and on line breaks. Also, you'll need the word-wrap property.
jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function. Syntax: $(selector).
The wrap() method wraps specified HTML element(s) around each selected element.
Use :contains
filter selector:
$('p.hidden:contains("click here")')
To wrap it in link:
$('p.hidden:contains("click here")').html()
.replace('click here', '<a href="url">click here</a>');
To wrap whole text in link:
$('p.hidden:contains("click here")')
.html('<a href="url">' + $('p.hidden:contains("click here")').text() + '<a>');
More Info:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With