deerox deerox - 1 year ago 109
Javascript Question

Change content of span by id

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

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

I am trying this but not working i have no clue why not working.

function set() {
document['getElementById']('user')['value'] = Owner;
// owner value is mike

Answer Source

If you want to change the id, use

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

or, more classically,

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

If you want to replace "John" (that is not the ID but the content of the span), do

 document.getElementById('user').innerHTML = 'mike'; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download