0

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.

0

4 Answers 4

4

Change it to

document.write("<div class='overview'><ul><li>" + item + "</li></ul></div>");
Sign up to request clarification or add additional context in comments.

6 Comments

And don't try to read the element before its been loaded in the document.
Ah thanks that works, However it seems to come up with "undefined" is that because the "this text" is still loading after the viewport has loaded?
Put the first script below the div. (i.e. make it the last thing on the page)
or is the $(document).ready() a possibility? as I have recently read up about it.
Yes, but you'll have to change around your script to work. You'd end up putting songlist (); into the $(document).ready() function. You'd then have to change your songlist function to be $('.viewport').innerHTML("<div class='overview'><ul><li>" + item + "</li></ul></div>")
|
3
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.

2 Comments

Hmm I will look into that because I was thinking of implementing this idea throughout my site. thanks. But, viewport is a class and not a id would the "getelementbyid" still work for that?
oops sorry, missed that. getElementById would not work on a class. Using jQuery however you could use $('.viewport') instead. You can use almost anything to get a DOM element in jQuery :)
0

Like this?

document.write("<div class='overview'><ul><li>" + item + "</li></ul></div>");

Comments

0

-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.net/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>

Comments

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.