I am wondering if its possible to change a HTML elements data type with javascript?
For example change an img to a div? The reason I am doing this & not destroying the img & creating a div is because the element contains A LOT of inline styling so I would need to copy all the inline style from the img to the div. PS: this website is only for iPad, so cross-browser compatibility is not an issue.
var div = img.cloneNode(true);
div.type = "div";
So it would be easier to clone the node/element using img.cloneNode(true), then must change the type to a div. I hope this is possible?
If not maybe there is a easy way to copy one elements style to another elements? Maybe...
// img is an img HTML element
var div = document.createElement("div");
div.style = img.style;
document.body.removeChild(img); // does that destroy div.style aswell?
The easiest way to modify the content of an HTML element is by using the innerHTML property . The innerHTML property gets or sets the HTML or XML markup contained within the element.
Add the repeat-element attribute to the element you want to duplicate and specify the number of times to copy. Use the repeat-text attribute if you only want to duplicate the text within the element.
Duplicate IDs are common validation errors that may break the accessibility of labels, e.g., form fields, table header cells. To fix the problem, change an ID value if it is used more than once to be sure each is unique.
You cannot change the type of an element. Yes, the div's style will be overwritten unless you concatenate div.style+=img.style. In this case like img style attributes override the div's.
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