0

UPDATE: I have double checked again my code. The problem with it is actually the placement of the function (which I didn't post it up here) and I have fixed it!

This is just a short summary of my code. But it just won't run even if I put the js after my label.

< script type = "text/javascript" >
  document.getElementById('lblSport').innerHTML = 'Hello'; < /script>
<label class="lblSport" id="lblSport">asas</label>

3
  • it works in stackoverflow snippet by removing the <script type = "text/javascript" > </script> Commented Feb 12, 2016 at 8:46
  • @himawanraharja yes that's the problem btw how can you edit and run the snippet here? Commented Feb 12, 2016 at 8:49
  • @Webinan well i "coppy snippet to answer", try to erase that and run it but i'm not posting it as answer Commented Feb 12, 2016 at 9:01

6 Answers 6

1

Delete The space before script tag and the end of the script tag and it will work.

<script> .... </script>
Sign up to request clarification or add additional context in comments.

Comments

1

The label is undefined in your case because your code is being called before the actual label exists on the page. Try adding your code inside a "document ready" function for this to work. The document ready function will fire when the document is ready (Once the document loaded all it's elements).

<script type = "text/javascript">
        $(document).ready(function () {
            document.getElementById('lblSport').innerHTML = 'Hello';
        });
</script>

<label class="lblSport" id="lblSport">asas</label>

Comments

0

You have to wait for the page to be loaded, remove space on script tags and haven't the same name for the class and id

window.onload = function(){
    document.getElementById('lblSport').innerHTML = 'Hello';
}


<label class="lblSports" id="lblSport">asas</label>

1 Comment

Yes, this is definitely an answer but I just found out that the actual problem with it is actually the placement of my function(which I didnt post it out here) ;)
0

Your code is fine, check this demo

window.onload = function(){
   document.getElementById('lblSport').innerHTML = 'Hello';
};

just ensure that element is loaded to DOM before your change its innerHTML

Comments

0

see this fiddle: https://jsfiddle.net/wk63pov0/

<label id="demo" >(innerHTML).</label>

Js:

document.getElementById("demo").innerHTML = " changed!";

please post your full html.

Comments

0

Your open and closing script html tags contain unneeded space. It should look like this:

<script></script>

As well, according to HTML5:

The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

http://www.w3.org/TR/html5/dom.html#the-id-attribute

While you can have the same name for class and ID, best practice would probably be to avoid using same names, mainly for readability.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.