Why do I get the error Uncaught TypeError: Cannot set property 'display' of undefined in Javascript? -


I am trying to understand the use of the document object type and challenge yourself I am trying to hide all the div tags when I execute the function, then error me unchecked type error returned: Property can not set 'display' to be undefined But unsure that its meaning is absolutely fine. What is undefined?

Code output

code

   & Lt; Meta http-equiv = "content-language" content = "en-us" / & gt; & Lt; Meta http-equiv = "cache-control" content = "no-cache" /> & Lt; Meta http-equiv = "pragma" content = "no-cache" /> & Lt; Meta name = "keyword" content = "" /> & Lt; Meta name = "description" content = "" /> & Lt; Meta name = "author" content = "" /> & Lt; Meta name = "copyright" content = "& amp; copy; 2012" /> & Lt; Meta name = "robot" content = "noindex, noophalo" /> & Lt; Title & gt; JS Features & lt; / Title & gt; & Lt; Base href = "" /> & Lt; Link rel = "stylesheet" type = "text / css" media = "all" href = "" /> & Lt; Style type = "text / css" media = "all" & gt; & Lt; / Style & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "container" & gt; & Lt; Div id = "header" & gt; & Lt; / Div & gt; & Lt; Div id = "content" & gt; & Lt; P & gt; This sample content is & lt; / P & gt; & Lt; / Div & gt; & Lt; Div id = "footer" & gt; & Amp; Copy; 2012 and lt; / Div & gt; & Lt; / Div & gt; & Lt; Script type = "text / javascript" & gt; Function hideMe () {// Leave all div elements var div = document.getElementsByTagName ("div"); For (var i = 0; i & lt; div.length; i = i + 1) {div.style.display = "none"; }} & Lt; / Script & gt; & Lt; P onClick = "hideMe ();" & Gt; Click to hide & lt; / P & gt; & Lt; / Body & gt; & Lt; / Html & gt;    

getElementsByTagName gives an array, then you need it :

  var divs = document.getElementsByTagName ("div"); For (var i = 0; i & lt; divs.length; i = i + 1) {divs [i] .style.display = "none"; }    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -