Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change output HTML tag <b></b> to <strong></strong>?

How to replace <b></b> tag with <strong></strong> tag to a specific div?

ex:

<div id="aaa">hello<b>wow</b>!</div>

using javascript to replace with

<div id="aaa">hello<strong>wow</strong>!</div>

please help! thanks in advance.

enter image description here

***** Why I'm try to do is change the output HTML code <b></b> to <strong></strong> , in order to get W3C validation. Can I do that? **

Or Is there any solution that can use ASP.NET+C# to do that?

like image 805
user951581 Avatar asked Dec 10 '22 06:12

user951581


1 Answers

Here you go:

var root, elems;

root = document.getElementById( 'test' );
elems = root.getElementsByTagName( 'b' );

toArray( elems ).forEach( function ( elem ) {
    var newElem = document.createElement( 'strong' );
    newElem.textContent = elem.textContent;
    elem.parentNode.replaceChild( newElem, elem );    
});

where toArray is your preferred array-like to array converter function. I use this one:

function toArray( arrayLike ) { return [].slice.call( arrayLike ); }

Live demo: http://jsfiddle.net/mJSyH/3/

Note: this code doesn't work in IE8.

like image 136
Šime Vidas Avatar answered Dec 21 '22 20:12

Šime Vidas