1.

Solve : Javascript: Alternative commands to document.write()?

Answer»

Hello!  I am a new programmer.  I am trying to display a variable, but the only WAY I know to do that with is the document.write() COMMAND.  Every time I do that, it wipes everything, and puts the variable in the top left corner.  Are there any other ways that I could possibly display a variable?  Thanks -SamHi SamZelda1

create a span element with id "id" then use document.getElementById('id').innerHTML = value;If you are trying to display a variable for debugging purposes you can use console.log(var)
This will write the value of the variable into the javascript console.
In chrome you get to from the keyboard shortcut Command + OPTION + J (MAC) or Control + Shift + J (Windows/Linux)
other wise feras has your answer.
Or you could use window.alert(var). Cumbersome, but does what you are LOOKING to do. Personally I like Waffle's method better.  Create a span element id and then use this code:
Code: [Select]  <script type="text/javascript">
        var myDiv1 = document.getElementById("myDiv1");
        var myDiv2 = document.getElementById("myDiv2");

        myDiv1.innerHTML = "<b>Content of 1st DIV</b>";
        myDiv2.innerHTML = "<i>Content of second DIV element</i>";
    </script>
</html>
Hope this helps.



Discussion

No Comment Found