1

I have this code :

<label class='textFontSmall center' for="usr" id="addNameText">BOB</label>

And I want the ability to change the text from BOB to Cliff. I have tried 4 different ways :

document.getElementById('addNameText').value = 'CLIFF';
document.getElementById('addNameText').text = 'CLIFF';
document.getElementById('addNameText').html = 'CLIFF';
document.getElementById('addNameText').innerHtml = 'CLIFF';

None of these work. How come ? I must be missing something simple

https://jsfiddle.net/rukzkaLe/

5 Answers 5

1

Use this :

document.getElementById('addNameText').innerText = 'CLIFF';

Or this :

document.getElementById('addNameText').innerHTML = 'CLIFF';
Sign up to request clarification or add additional context in comments.

1 Comment

Please put the code in code blocks (use 4 spaces to indent the code - see my edit). Welcome to StackOverflow!
0

jsfiddle is not a good tool in this case, cause the JavaScript has to be executed after the html:

<label class='textFontSmall center' for="usr" id="addNameText">BOB</label>

You have an example here : http://www.w3schools.com/js/tryit.asp?filename=tryjs_change_innerhtml

2 Comments

i looked at that link but obviously I missed typed it. I typed 'innerHtml' rather than 'innerHTML'. Freaking idiot i am. Cheers man
JavaScript can be executed synchronously on JSFiddle (example).
0

Use this $("#addNameText").text('CLIFF');

1 Comment

not using JQuery at this moment, but thanks anyway :) I've solved it, was a typo :( answered in comments of another answer
0

How about this --

$(document).ready(function(){ $('#addNameText').html('CLIFF'); });

Comments

0

After changing the settings of JS Fiddle's second <select> list to "no wrap - in <body>" (which means the <script> containing the supplied JavaScript (from the JavaScript panel) is inserted into the <body> element, as the last-child before the </body> closing tag (and therefore is executed after the DOM is constructed, you can simply use:

document.getElementById('addNameText').textContent = 'CLIFF';

JS Fiddle demo.

Or, correcting the typo in your call to innerHTML (you used the camel-case: innerHtml, which is non-existent property of an HTMLElement):

document.getElementById('addNameText').innerHTML = 'CLIFF';

JS Fiddle demo.

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.