Uncaught TypeError on second onclick event using this.innerHTML -
Everyone, I have a very strange problem.
In an HMTL anarded list I have several list elements with the OnClick event, and they all call the same function.
& lt; Ul & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); alerts (this.inner html);" & Gt; 1 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); alerts (this.inner html);" & Gt; 2 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); warning (this.inner html);" & Gt; 3 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.inner html); warning (this.inner html);" & Gt; 4 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); warning (this.inner html);" & Gt; 5 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.inner html); warning (this.inner html);" & Gt; 6 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); warning (this.innerHTML);" & Gt; 7 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); warning (this.inner html);" & Gt; 8 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.innerHTML); warning (this.innerHTML);" & Gt; 9 & lt; / Li & gt; & Lt; Li onClick = "javascript: show (this.inner html); alerts (this.inner html);" & Gt; 0 & lt; / Li & gt; & Lt; / Ul & gt;
This is a javascript function:
function show (id) {show = document.getElementById (ID); NotShow = document.getElementsByClassName ("visible") [0]; If (type notShow! == "undefined") {notShow.classList.toggle ("hidden"); NotShow.classList.toggle ("visible"); } Show.classList.toggle ("hidden"); Show.classList.toggle ("visible"); }
For some unknown reasons, when I & lt; Li & gt;
Clicks on one of the elements, but the second time when I do this, the function works fine an error:
Uncount Type error: Object function ACNL.php not Is: 31
I think the error is not inside the Java Script function, but the HTML-element that calls the function.
Any help would be appreciated!
I see some problems here. In a special order:
- This will probably be the safest to change the internal variable name
show
because your function will beshow (... >.
and- To avoid popularizing the top namespace, declare variables containing the keyword
var
.- You are retrieving the DOM elements by ID , But none of your DOM elements (in the example above) are id attributes. You can call them your
li
If you do not havevisible
next to these elements, you can add both < Code> Visiblehidden
when you "Toggle". - To avoid popularizing the top namespace, declare variables containing the keyword
- If you type
visible
andhidden
items, then
notShow = document.getElementsByClassName ("visible") [0];
must possibly be changed, because youli
Once you receive the item, you will retrieve them atvisible
Try to use names or element types of other classes.
This is the one to get you started (ignore the interpretation of the window which is specific to jsFiddle).
Comments
Post a Comment