Skip to content Skip to sidebar Skip to footer

Innerhtml Closes Tags

I have the following switch structure: switch (ratio) { case '16:9': imgContainer.innerHTML = '' break cas

Solution 1:

You're on the right path. The reason this behavior was happening in the first place is that once you set that HTML of the live DOM element on the page, the browser is going to try to "correct" any broken HTML for you. This includes closing tags.

As you intend, the solution is to put all of your strings together first and then set the result to the HTML of the element. But in your attempt at least one problem is that you're trying to re-declare the same variable over and over.

You don't really need the variable. You can simply build the string all on one line of code and set it to the HTML. Something as simple as this:

imgContainer.innerHTML = '<imgsrc="images/schita-usi-1.jpg"usemap="#schita-usi-1">'
                       + '<mapname="schita-usi-1">'
                       + '<areashape="rect"coords="191,108,577,973"href="test.html"alt=""title="">'  
                       + '</map>';

That gives you the multi-line formatting you want in the code (I assume for readability), but performs the entire operation as a single assignment. Everything to the right of the = will be evaluated first (all of the "lines", even though until the semi-colon it's all one "line of code"), and then the result is assigned to the left of the =.

Solution 2:

You need to remove the subsequent var keywords after the first line so it becomes:

var htmlcode = '<imgsrc="images/schita-usi-1.jpg"usemap="#schita-usi-1">';
htmlcode += '<mapname="schita-usi-1">';
htmlcode += '<areashape="rect"coords="191,108,577,973"href="test.html"alt=""title="">';
htmlcode += '</map>';

After you have declared a variable with var, don't use the var keyword again for the same variable.

Post a Comment for "Innerhtml Closes Tags"