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.


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


<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>


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

jsFiddle example

Need Your Help

Creating audit trail with trigger in MySQL

mysql sql triggers audit-trail

I want a trigger that triggers whenever the loan table is updated (i.e. a book is returned). It should take values from the rows in the loan table only where the loan is overdue and insert them int...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.