+ 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.
5 Antworten
+ 4
no your code returns the element not the content
document.getElementById("a").innerHTML is the right
do that code
actually innerHTML needs string
+ 1
var h1Content = document.getElementById('q').innerHTML;
alert(h1Content) ;
+ 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>
+ 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
+ 1
First id=“q”
Second var a=document.getELementById("q")
alert("a")
Ok~