+ 1

How to get a string in h1 tag to js ?

<h1 id=q> hi </h1> <script type=text/JavaScript> var a=document.getelementbyid="q"; alert(a); </script>. how can I run this one without error.

24th Nov 2016, 5:45 AM
Jithin Sathianandan
Jithin Sathianandan - avatar
5 odpowiedzi
+ 4
no your code returns the element not the content document.getElementById("a").innerHTML is the right do that code actually innerHTML needs string
24th Nov 2016, 12:00 PM
Sandeep Chatterjee
+ 1
var h1Content = document.getElementById('q').innerHTML; alert(h1Content) ;
25th Nov 2016, 11:57 AM
brian cersosimo
brian cersosimo - avatar
+ 1
your id is not right. put "" around your id and then use it. also there is an error in your script code. your line after script tag is wrong you can do it as like following <h1 id="q"> hi </h1> <script type="text/javascript"> var a = document.getElementById("q"); alert(a); </script>
25th Nov 2016, 4:44 PM
Zeeshan
Zeeshan - avatar
+ 1
you said right zeeshan it should be "" but that's not the problem it runs even without that they are optional provided the value doenot contain space problem is use of. element instead of element.innerHTML
25th Nov 2016, 4:46 PM
Sandeep Chatterjee
+ 1
First id=“q” Second var a=document.getELementById("q") alert("a") Ok~
1st Dec 2016, 2:58 PM
Leo
Leo - avatar