John I ...">

Change the contents of a range by ID

I am trying to change john to mike. I have no idea why it is not working.

<span id="user">John</span> 

I try this, but I don’t work, I have no idea why it doesn’t work.

 function set() { document['getElementById']('user')['value'] = Owner; // owner value is mike } 
+7
source share
5 answers

If you want to change the identifier, use

  document['getElementById']('user').id = 'mike'; 

or, more classically,

  document.getElementById('user').id = 'mike'; 

If you want to replace "John" (this is not an identifier, but the contents of a range), do

  document.getElementById('user').innerHTML = 'mike'; 
+13
source
 function set() { document.getElementByID('user').innerHTML = Owner; // owner value is mike } 
+3
source

You can try this

 function set() { var elem = document.getElementById('user'); elem.innerHTML = "Owner"; } if you want to add an **id** you can use **setAttribute()** eg: document.getElementById('user').setAttribute('id','owner'); 

Note

  **value** attribute only work with input, text area,button etc.. 

eg:

  <input type="text" id="inid" value=""/> document.getElementById('inid').value = "Something"; // this will work 
+2
source

to try:

 function set() { document.getElementById('user').innerText= Owner; // owner value is mike } 

Where is Owner declared, is it valid in your function scope?

+1
source

For modern browsers

 document.getElementById("span_id_here").textContent="yourtext"; 
0
source

All Articles