1

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
  • <input/> tags display their value and don't display what's in them (as they're empty) - i.e. if you did this: <input type="text">Lorem ipsum</input> this would display nothing as <input/>s are not designed to show their contents. Most other elements, like <span>s show their contents, not their "value" attribute. Commented Jul 22, 2010 at 12:03

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

elem_pop.innerHTML = "coming";

2 Comments

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: jsfiddle.net/nick_craver/ehtX8
@Nick Craver - of course there isn't. Fingers too used to camel casing... Answer updated.
1

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.

5 Comments

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...
@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?
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: 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?

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.