Change ID to text in paragraph with jQuery and Replace spaces with underscores

I would like to take what text is in my p tag and make that the p tags parent (div) that ID. I would also like to add a _ for any spaces in the p tag.

Example:

<div class="circle"><p>Apple</p></div>
<div class="circle"><p>Banana</p></div>
<div class="circle"><p>Carrot Juice</p></div>

to

<div id="Apple" class="circle"><p>Apple</p></div>
<div id="Banana" class="circle"><p>Banana</p></div>
<div id="Carrot_Juice" class="circle"><p>Carrot Juice</p></div>

Answers


$('div.circle p').each(function() {
    $(this).parent('div').attr('id', $(this).text().replace(/ /g,'_'));
});‚Äč

jsFiddle example


Need Your Help

reinterpret_cast to QObject's subling

c++ qobject reinterpret-cast

I've got some kind of object factory (template based), that works pretty good for my purposes. But now I've tried to work with class, that derives from both QObject and pure abstract class (interfa...