tags:

views:

38

answers:

3

I am using javascript setAttribute for following to change 'lorem ipsum' to 'coming' in the following manner :

<span id="pop_title" name="pop_title" class="title01_mc10401 ff_1 fs_10 fc22 fsb">lorem ipsum</span>

Script:

function modifyDOM(){
  var elem_pop = document.getElementById("pop_title");
  elem_pop.setAttribute("value","coming");
}

When I inspect in firebug, its showing that the value attribute is added with coming, but its reflecting in the browser (I am using mozilla 3.0.5) What may be the issue ?? do i need to set different attribute ??

+1  A: 

Use .innerHTML instead here, like this:

function modifyDOM(){
  var elem_pop = document.getElementById("pop_title");
  elem_pop.innerHTML = "coming";
}

You can try it here. Setting the value attribute would be for something like an <input> element, when you want to replace the html within an element's tags, use .innerHTML instead.

Nick Craver
but when i added elem_pop.setAttribute("value","coming"); in the function then <span> tag is still the same value attribute is not added (in your editor)Its working in your editor, but still the problem exists in mine side
@kalxindia - `<span>` has no value attribute...if you want to change the text inside use `.innerHTML` as I have above...
Nick Craver
i have used innerHTML, but still its not working
@kalxindia - Without more data I can't say...this works on your example code, as the demo confirms (this is a very common thing), do you have multiple elements with the same ID or anything?
Nick Craver
i have one more span tag inside it as :<span id="pop_title" name="pop_title" class="title01_mc10401 ff_1 fs_10 fc22 fsb">AIR-GROUND TELEPHONE:<span class="fc5"> TERMS OF USE </span></span>
@kalxindia - It also works for that: http://jsfiddle.net/nick_craver/sNAq6/1/ You have something *else* happening outside of the info you've posted...what's "not working", are you getting any JavaScript errors in your console?
Nick Craver
+2  A: 

The span element does not have a value attribute. Try setting its innerHTML instead:

elem_pop.innerHTML = "coming";
Oded
There isn't a `.innerHtml` property, this will result in setting a new DOM property you're creating, but have no effect on the text, try it here: http://jsfiddle.net/nick_craver/ehtX8/
Nick Craver
@Nick Craver - of course there isn't. Fingers too used to camel casing... Answer updated.
Oded
A: 

There are two ways to do this (correctly). You can use this cross-browser method here:

elem_pop.appendChild( document.createTextNode('coming') );

The other way is to use elem_pop.textContent and/or elem_pop.innerText, but these unfortunately don't work cross-browser. IE only supports innerText, Firefox, Chrome only support textContent - Opera is the only one that supports both.

You could use elem_pop.innerHTML, HOWEVER be aware that innerHTML inserts HTML markup, NOT just text into the page. This means that it won't escape any special characters into entities. It also replaces any child nodes automatically and can destroy event listeners.

I'd recommend the first (appendChild()) method, but if you want to use textContent/innerText you could place this at the top of your script:

HTMLElement.prototype.__defineGetter__('innerText', function(){ return this.textContent; });
HTMLElement.prototype.__defineSetter__('innerText', function(t){ this.textContent=t; });

That will give Firefox innerText support and you can use it all you like.

lucideer