Let's say I wanted to change all <img>
tags to <iframe>
tags on a page. Could javascript be used for this purpose?
Is there any way to change the actual tag type, or would I have to first delete the <img>
and then create the <iframe>
, if so, how can I make sure that the new tag has the same container as the old tag, etc?
What's the most straightforward and browser friendly way to perform such a substitution?
The HTML DOM allows JavaScript to change the style of HTML elements.
Use the textContent property to change the text of a link element, e.g. link. textContent = 'Replacement link text' . The textContent property will set the text of the link to the provided string, replacing any of the existing content.
Although it is more convenient using a library like jQuery
, you can do it with no library like this (replacing the element, not changing the type):
Example: http://jsfiddle.net/BvSvb/
var imgs = document.getElementsByTagName('img');
var i = imgs.length;
var parent;
var iframe = document.createElement( 'iframe' );
var currFrame;
while( i-- ) {
currFrame = iframe.cloneNode( false );
currFrame.setAttribute( 'src', imgs[ i ].getAttribute( 'src' ) );
parent = imgs[ i ].parentNode;
parent.insertBefore( currFrame, imgs[ i ] );
parent.removeChild( imgs[ i ] );
}
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