So recently I discovered that document.getElementById(hello).innerHTML and hello.innerHTML gives me the same effect in a javascript code. So why use the first one if the second is viable? Is there a risk of using just hello.innerHTML?
Asked
Active
Viewed 28 times
0
-
What is `hello` exactly? I assume it's a variable...if so, what is its value? – skippr Nov 18 '15 at 22:38
-
I bet you mean `getElementById('hello')`... – Felix Kling Nov 18 '15 at 22:42
-
@FelixKling no i mean in an html there is an element with id="hello" and in javascript in one case i state a var (lets say var meow) =document.getelementbyid("hello") and i use meow.innerHTML = "poop". in the other case I dont state a variable at all to define the id and i just use the id itself to change the innerhtml (since the id is hello) i will put hello.innerHTML. hello is not a variable or anything, its just an id within the html code, and It works! – King Lynx Nov 18 '15 at 23:04
-
@dhira no i mean in an html there is an element with id="hello" and in javascript in one case i state a var (lets say var meow) =document.getelementbyid("hello") and i use meow.innerHTML = "poop". in the other case I dont state a variable at all to define the id and i just use the id itself to change the innerhtml (since the id is hello) i will put hello.innerHTML. hello is not a variable or anything, its just an id within the html code, and It works – King Lynx Nov 18 '15 at 23:05
-
You wrote in your question `getElementById(hello)`, which means passing the **variable** `hello` to `getElementById`. As you confirmed, you are passing a string instead (`getElementById('hello')`) which was the whole point of my comment. Passing a string is very different from passing a variable though. Please make sure the information in your question also reflects what you are doing. – Felix Kling Nov 18 '15 at 23:19