1

i created a javascript and did all the coding required and finally got a variable "bookerVar" i want this variable to be displayed at the bottom of my html page . How should i do it.

(longer explanation: i created a page where a person can choose from a drop down as he chooses from the drop down using javascript i do certain calculatons and finally store the last value in a variable called bookerVar which is in a function called bookersay().

the last line of the function says return bookerVar.

now what i want is that the value stored in bookerVar can be displayed at the bootom of my html page in a div tag). can anyone tell me what do i have to write in my html code and my javascript (which is actually in a different file). Thanks.

2
  • could we see the script? Commented Jul 25, 2011 at 20:06
  • its actually not complete and would make little sense now but i would upload it later so that in future if someone comes to this page , it could be of help Commented Jul 25, 2011 at 20:15

3 Answers 3

3

Place this in the .html doc that the bookerVar is currently located.

<script type="text/javascript">
    function addDiv() {
        var div = document.createElement('div');
        div.innerHTML = bookerVar;
        document.getElementsByTagName('body')[0].appendChild(div);
    }
    addDiv();
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

do i also have to do anything in the div tag in the html file in which i want it to show up. ??
No, I depending on what bookerVar is, I would suggest surrounding it in a <p> tag or <span> tag.
1

See if this example that i created fits you. You can copy/paste in in some .html file and run it.

<html>
<head>
<title>Appending content to some div</title>
</head>     
<body>
<div id="content"></div>
</body>
<html>

<script type="text/javascript">
function appendToContent(html) {
    var content = document.getElementById('content');
    var div = document.createElement('div');
    div.innerHTML = html;
    content.appendChild(div);
}

appendToContent('hello world');
</script>

4 Comments

a small question my javascript is on another page and also can you please explain it using the variable bookerVer , ididnt understand the script after var div = document.createElement('div');
This was just a 'generic' example. The document.createElement('div') creates a new element, that is a div, and next, it appends some html to it. If you want to append the value of your bookerVer variable, just call appendToContent(bookerVer). Of course, you might need to do some changes to adapt this code to your page.
i am actually learning javascript by tutorials so i just know very little stuff. so all i have to do is replace the last line of code to appendToContent(bookerVar)?? Thank you so much for your help .
Yes, but i don't know how your code is structured, you might need to do some slight changes to my code.
0

The easiest thing to tell you is to get JQuery, then use something like this:

$("body").append("<div>" + bookerVar + "</div>");

3 Comments

There is no mention of jquery..?
actually pure DOM would be easier imo.
i just know enough javascript to do what i mentioned before. I am learning javascript by using tutorials and have no idea of jquery. But still thanks.

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.