dannysood
dannysood

Reputation: 1139

javascript function display in a div

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.

Upvotes: 1

Views: 3774

Answers (3)

Phil
Phil

Reputation: 11175

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>

Upvotes: 3

user859749
user859749

Reputation:

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>

Upvotes: 1

John Fisher
John Fisher

Reputation: 22719

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

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

Upvotes: 0

Related Questions