I’m trying to add some elements dynamically using javascript DOM. I’m successfully creating the elements with javascript DOM which must have ending tag. I don’t know how to create an element which does not have ending tag like img element.
Here’s how i am doing this:
<html>
<head>
<script>
function add(){
var myImg = document.createElement("img");
myImg.setAttribute("src","c:\abc.png");
myImg.setAttribute("width","20");
myImg.setAttribute("height","20");
var myDiv = document.getElementById("mydiv");
myDiv.appendChild(myImg);
}
</script>
</head>
<body >
<div id="mydiv">
<button onclick="add();">Add</button>
</div>
</body>
</html>
By doing this, it creates the IMG element with ending tag too. See below what i am talking about.
<img src="c:\abc.png" width="20" height="20"></img> //Incorrect(Ending tag available)
This is what i want:
<img src="c:\abc.png" width="20" height="20">
Please help me with this. Thanks.
Since you’re creating this with JavaScript, you’re probably looking at the developer console to see the code (such as closing tags). That’s all generated by the browser and includes fixes the browser makes to your code. You shouldn’t worry about this, since it’s mostly out of your control.