javascript setAttribute - Stack Overflow

I am using javascript setAttribute for following to change 'lorem ipsum' to 'ing' i

I am using javascript setAttribute for following to change 'lorem ipsum' to 'ing' 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","ing");
}

When I inspect in firebug, its showing that the value attribute is added with ing, 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 ??

I am using javascript setAttribute for following to change 'lorem ipsum' to 'ing' 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","ing");
}

When I inspect in firebug, its showing that the value attribute is added with ing, 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 ??

Share Improve this question edited Jul 22, 2010 at 11:20 Nick Craver 631k138 gold badges1.3k silver badges1.2k bronze badges asked Jul 22, 2010 at 11:18 Kalpesh JainKalpesh Jain 4093 gold badges10 silver badges19 bronze badges 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. – lucideer Commented Jul 22, 2010 at 12:03
Add a ment  | 

3 Answers 3

Reset to default 2

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

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

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 remend 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.

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

elem_pop.innerHTML = "ing";

Use .innerHTML instead here, like this:

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208609a4616711.html

相关推荐

  • javascript setAttribute - Stack Overflow

    I am using javascript setAttribute for following to change 'lorem ipsum' to 'ing' i

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信