Raziel
Raziel

Reputation: 1566

Remove strictly strong/bold text tag from html via Jquery

currently having a slight problem with respect to removing strong tags using jquery.

I want to retain the text between the strong tags, so eg: before:

<strong>This is a text</strong>

After:

this is a text

and Code:

    $("div.row.ordertype").each(function(){
           if(($(this).text()).length>0){
            console.log($(this).html());
            var test = $("<strong>","<strong />").contents().unwrap();
            console.log(test);
           }    
    })

any ideas

Upvotes: 2

Views: 1882

Answers (2)

user3146115
user3146115

Reputation:

Use below jQuery

$('strong').contents().unwrap();

For faster performance, always use below:

var strong = document.getElementsByTagName('strong');
while(strong.length) {
    var parent = strong[0].parentNode;
    while( strong[0].firstChild ) {
        parent.insertBefore(strong[0].firstChild, strong[0]);
    }
     parent.removeChild(strong[0]);
}

This will more faster than any jQuery solution.

Upvotes: 3

adeneo
adeneo

Reputation: 318312

Should be

$("strong").contents().unwrap();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>This is a text</strong>

The selector $("<strong>","<strong />") creates new elements, it doesn't select elements

Upvotes: 2

Related Questions