Roger
Roger

Reputation: 8586

Find and Replace child elements with JQuery

I wish to make a find and replace operation (from '*' to '+') via JQuery in a list of child elements.

Sample code:

<div class="tt">
    <p>*Anything</p>
    <p>*Anything</p>
</div>

Desirable output:

<div class="tt">
    <p>+Anything</p>
    <p>+Anything</p>
</div>

This alerts me each 'p' tag changed:

$('.tt p').each(function(index) {
    var r=/^(<p>)(.)(.*)$/;
    var to_r='$1+$3';
    //var a=p.match(r);
    var a=p.replace(r,to_r);
    alert(a);
});

My JQuery skills end here. Thanks for any directions.

Upvotes: 1

Views: 1428

Answers (2)

KaiserKoenig
KaiserKoenig

Reputation: 35

tt.each refers to the div, not to the p-tags. so try something like

$('.tt').find('p').each(function(){ $this.html('YourNewHTML or text')})

or use your regEx

Upvotes: 2

Bla...
Bla...

Reputation: 7288

Try out this code:

$('.tt p').each(function(index) {
    var result = $(this).text().replace("*", "<b>+</b>");
    $(this).html(result);
});

Upvotes: 1

Related Questions