admin管理员组文章数量:1291147
I have a html page, inside which i have following code snippet, depending on which case we're following:
CASE 1) <p> <span id ="xyz"> some code here </span> </p>
CASE 2) <p> <span id ="xyz"> </span> some code here </p>
Now in my javascript code I have to write values in span xyz
dynamically. If I try to do get reference to span xyz
by id and then try to alter its innerHTML, then the html already present in span xyz
is lost.
If I keep extra code outside the span, it appears on new line due to some css effects. I cannot alter css due to some reasons.
I have a html page, inside which i have following code snippet, depending on which case we're following:
CASE 1) <p> <span id ="xyz"> some code here </span> </p>
CASE 2) <p> <span id ="xyz"> </span> some code here </p>
Now in my javascript code I have to write values in span xyz
dynamically. If I try to do get reference to span xyz
by id and then try to alter its innerHTML, then the html already present in span xyz
is lost.
If I keep extra code outside the span, it appears on new line due to some css effects. I cannot alter css due to some reasons.
Share Improve this question edited Dec 20, 2010 at 15:34 Konerak 39.8k12 gold badges101 silver badges121 bronze badges asked Dec 20, 2010 at 15:27 akshayakshay 711 gold badge3 silver badges8 bronze badges 2- Your question is not clear. One thing I will note: do not use the same "id" value on more than one element. (I can't even tell if that's what you're doing, however.) – Pointy Commented Dec 20, 2010 at 15:29
- i just mentioned 2 possible ways 2 do it , and problem am facing with each approach – akshay Commented Dec 20, 2010 at 15:51
4 Answers
Reset to default 3You can just store the current value in a String, and then modify this string:
var mySpan = document.getElementById('xyz').innerHTML;
mySpan += ' and this gets added after the some code here';
document.getElementById('xyz').innerHTML = mySpan;
or faster and more shorthand,
document.getElementById('xyz').innerHTML = document.getElementById('xyz').innerHTML + ' new text after'; //to add text after the existing text
document.getElementById('xyz').innerHTML = 'your new text before ' + document.getElementById('xyz').innerHTML; //to add text before.
You can append a new text node to span, if you want to keep the old text.
var newtext = document.createTextNode(" new text ");
var spanXyz = document.getElementById("xyz");
spanXyz.appendChild(newtext);
Refer these: createTextNode, appendChild
Edit: To add new text at the beginning, you can use something like
spanXyz.textContent = "new text " + spanXyz.textContent;
If you are not using jquery :
document.getElementById('xyz').innerHTML= document.getElementById('xyz').innerHTML + "XYZ";
If you are using jquery:
$("#xyz").append("xyz");
document.getElementById('xyz').innerHTML = 'some code here' + 'dynamically code';
or
document.getElementById('xyz').innerHTML = 'dynamically code' + 'some code here' ;
本文标签: htmlUsing javascript to alter span text dynamically and keeping the original textStack Overflow
版权声明:本文标题:html - Using javascript to alter span text dynamically and keeping the original text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503700a2382195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论