Change content of span by id

deerox picture deerox · Oct 22, 2012 · Viewed 42.6k times · Source

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

Denys S&#233;guret picture Denys Séguret · Oct 22, 2012

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';