I was developing some code and had an interesting issue
I have placed checkbox's inside a labels for a number of projects with no issues .. using straight html - not javascript. There should be no difference.
I spent several hours searching the web - as far as I can tell, I am the only person with these issues.
This page contains a number of examples - most will work as expected.
To be clear, this page, and the one that was failing, has
<!DOCTYPE html> |
In the final analysis (now that I think I understand the real issue), this has nothing (and everything) to do with the label tag. The problem is that using innerHTML to add information to any tag causes anything DOM object inside that tag to be recreated - thus losing all configuration data. However,the only tag I considered doing this with is the label tag - because I wanted to associate text with a checkbox. So, the label tag is not the problem, but it is the only tag where I am likely to see this problem.
Basic Test
No <label> | With <label> |
| |
---|---|---|---|
some text | |||
red when checked | red when checked |
In all my other code, this provides that reference - in this case, it refers to the window object.
Creating Components via Code
Components are generated by code | ||
---|---|---|
No <label> | With <label> | |
red when checked | red when checked | Event test - changes color when the event works |
var cell2 = row.insertCell(1); var label = document.createElement("label"); var checkbox1 = document.createElement("input"); checkbox1.type = "checkbox"; checkbox1.checked = true; // for test - it works cell2.appendChild(label); label.appendChild(checkbox1); // at this point everything is fine, the box is checked label.innerHTML += "Click me" + " "; // this is the line that breaks the links checkbox1.onclick = with_label_auto_Clicked; // checkbox1 is no longer associated with *label* |
label.innerHTML += "Click me" + " "; // this is the line that breaks the links |
According to w3schools, the "correct" way to accomplish this is to use the following.
var text1 = document.createTextNode("Click me" + " "); label.appendChild(text1); // this fixes the problem - except that " " is displayed |
Next I tried setting the innerHTML of the TextNode - silly me, TextNode's don't have an innerHTML - no change.
var text1 = document.createTextNode("Click me" + " "); label.appendChild(text1); // this fixes the problem checkbox1.onclick = with_label_auto_Clicked; text1.innerHTML += "Click me" + " "; // this failed, TextNode's don't have an innerHTML |
// This fixes the problem span = document.createElement("span"); label.appendChild(span); checkbox1.onclick = with_label_auto_Clicked; // This works span.innerHTML += "Click me" + " "; // Finally - success !!! |
Postscript
I checked these issues on several browser - When changing the innerHTML to add text to a label tag, they all failed the same.
The other issue is very troubling - on this page, in some of the event calls, the keyword this points to the window, in others, it points to the checkbox.
In all cases, the checkboxes are inside a table.
The difference appears to be how the event's is added
Before I found the real problem
var checkboxOnClick = function(event){ var series = this.mcSeries; if (this.childNodes[0].checked){ series.style.strokeStyle = "#00FF00"; }else{ series.style.strokeStyle = "#FF0000"; } g1.show(); } |
this.childNodes[0].checked |
In my opinion, using the span technique described in the previous section is much better (easier to understand and maintain).
Author: Robert Clemenzi