admin管理员组文章数量:1323734
This is probably a simple question ( or I assume to be ) however, I still can't get it to work no matter how much i twinker with it. I am trying to write a variable inside a li tag. This variable should hold the text within another div tag like so:
<script type="text/javascript">
var item = document.getElementById('thistext').innerHTML;
function songlist () {
document.write("<div class='overview'><ul><li> item </li></ul></div>");
}
</script>
<div id="thistext">hello</div>
<!-- I want it to appear here -->
<div class="viewport">
<script type="text/javascript">
songlist ();
</script>
</div>
So basically I need the text within "thistext" div tag to appear within the li tag which is within the ul etc (I don't know if thats unnecessary info or not ) I've also tried putting the script and its function within the viewport tag itself and with:
document.getElementById("id").innerHTML = "value";
that layout too but it doesn't appear to be showing working and I can't seem to understand why. I'm looking forward to the responses and thanks in advance.
This is probably a simple question ( or I assume to be ) however, I still can't get it to work no matter how much i twinker with it. I am trying to write a variable inside a li tag. This variable should hold the text within another div tag like so:
<script type="text/javascript">
var item = document.getElementById('thistext').innerHTML;
function songlist () {
document.write("<div class='overview'><ul><li> item </li></ul></div>");
}
</script>
<div id="thistext">hello</div>
<!-- I want it to appear here -->
<div class="viewport">
<script type="text/javascript">
songlist ();
</script>
</div>
So basically I need the text within "thistext" div tag to appear within the li tag which is within the ul etc (I don't know if thats unnecessary info or not ) I've also tried putting the script and its function within the viewport tag itself and with:
document.getElementById("id").innerHTML = "value";
that layout too but it doesn't appear to be showing working and I can't seem to understand why. I'm looking forward to the responses and thanks in advance.
Share Improve this question asked Sep 6, 2012 at 14:53 RoyRoy 3,2875 gold badges31 silver badges45 bronze badges 04 Answers
Reset to default 4Change it to
document.write("<div class='overview'><ul><li>" + item + "</li></ul></div>");
document.write("<div class='overview'><ul><li> item </li></ul></div>");
needs to be
document.write('<div class="overview"><ul><li>' + item + ' </li></ul></div>');
Edit:
I feel like I should point out that this isn't the best way to achieve this. Maybe something like this would better...
<script type="text/javascript">
window.onload = function(){
document.getElementById('viewport').innerHtml = document.getElementById('thistext').innerHtml;
};
</script>
Saying that, have you looked in the jQuery framework at all? It makes DOM manipulation very easy. There is a time and a place for it though. If all you're doing is replacing the text in one <div>
with the text from another <div>
then maybe its not appropriate.
Like this?
document.write("<div class='overview'><ul><li>" + item + "</li></ul></div>");
-now it works: You are missing '' in getElementById and teh function has to be moved after the div and you have to place the variable with surrounded by +''+ in the document write
jsfiddle: http://jsfiddle/fMNWt/
<div id="thistext">hello</div>
<script type="text/javascript">
var item = document.getElementById('thistext').innerHTML;
function songlist () {
document.write("<div class='overview'><ul><li>" + item + "</li></ul></div>");
}
</script>
<!-- I want it to appear here -->
<div class="viewport">
<script type="text/javascript">
songlist ();
</script>
</div>
本文标签: htmlHow do I write a variable in javascript from getelementbyidStack Overflow
版权声明:本文标题:html - How do I write a variable in javascript from getelementbyid - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119381a2421625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论