LJA
LJA

Reputation: 25

Replacing an element with specific text

I'm trying to change all dd's with specific text, in this example "color" from dd to dt elements.

   <dl>
    <dd><a>color</a></dd>
    <dd><a>green</a></dd>
    <dd><a>blue</a></dd>
   </dl>

   <dl>
    <dd><a>color</a></dd>
    <dd><a>red</a></dd>
    <dd><a>orange</a></dd>
   </dl>

What would be the best way to go about this?

Thanks

Upvotes: 0

Views: 48

Answers (2)

adeneo
adeneo

Reputation: 318182

$('dd').each(function() {
    if ( $.trim($(this).text()) == 'color' ) 
        $(this).replaceWith($('<dt>' + this.innerHTML + '</dt>'));
});

FIDDLE

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try using wrap()/unwrap()

$('dl dd').filter(function(){
    return $.trim($(this).text()) == 'color'
}).wrapInner('<dt />').contents().unwrap()

Demo: Fiddle

Upvotes: 1

Related Questions