Change shortcut text using javascript

Why does this not work for me?

<script> document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!'; </script> <label id="lbltipAddedComment"></label> 
+46
javascript html innerhtml label
Dec 20 '10 at 10:23
source share
6 answers

Since your script is running before there is a label on the page (in the DOM). Either put the script after the label, or wait until the document is fully loaded (use the OnLoad function, such as jQuery ready() or http://www.webreference.com/programming/javascript/onloads/ )

This will not work:

 <script> document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!'; </script> <label id="lbltipAddedComment">test</label> 

This will work:

 <label id="lbltipAddedComment">test</label> <script> document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!'; </script> 

This example (jsfiddle link) maintains order (script first, then label) and uses onLoad:

 <label id="lbltipAddedComment">test</label> <script> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } addLoadEvent(function() { document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!'; }); </script> 
+86
Dec 20 '10 at 10:28
source share

Have you tried .innerText or .value instead of .innerHTML ?

+14
Dec 20 '10 at 10:29
source share

Because the label element is not loading when the script is executed. Swap label and script, and it will work:

 <label id="lbltipAddedComment"></label> <script> document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!'; </script> 
+12
Dec 20 '10 at 10:28
source share

Using .innerText should work.

 document.getElementById('lbltipAddedComment').innerText = 'your tip has been submitted!'; 
+6
Jan 09 '16 at 9:41
source share

Use .textContent instead.

I also tried changing the label value until I tried this.

If this does not decide to try checking the object to find out what properties you can set by registering it in the console using console.dir , as shown on this question: How can I log an HTML element as a JavaScript object?

+3
Jul 29 '16 at 8:33
source share

Because the script will be executed first .. When the script receives the execution, the time controls are not loading. After loading the controls you write the script, it will work

0
May 16 '12 at 7:05
source share