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.
***** 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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With