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 0
Add a ment  | 

4 Answers 4

Reset to default 4

Change 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