Solved

displaying results of JavaScript in a div tag

Posted on 2011-09-15
11
336 Views
Last Modified: 2012-08-13
I need to display the returned string in HTML.

I have a JavaScript function which creates a string. Then I want to display it in an HTML span tag?

How?
0
Comment
Question by:newbieweb
  • 6
  • 4
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36545001
var myresults = "RESULT HERE";
$("#you_div_or_span_ID").html( myresults );
0
 

Author Comment

by:newbieweb
ID: 36545040
No, I need a div tag that can display the returned valud from a function

function MyMessage() {
   return "Hello";
}

What's the HTML
0
 

Author Comment

by:newbieweb
ID: 36545047
<span> ??? </span>
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 82

Expert Comment

by:leakim971
ID: 36545074
$("#you_div_or_span_ID").html( MyMessage() );

http://jsfiddle.net/CCWFE/
0
 

Author Comment

by:newbieweb
ID: 36545164
I do  not want a message box. I havee the box all taken care of. I want to display the returned valud from JavaScrip in HTML...


<span> returnStr() </span>

what's the format for calling returnStr() ??? It's a function.
0
 

Author Comment

by:newbieweb
ID: 36545174
It looks right, but where does your line of code go??

I am stuck.
0
 
LVL 2

Assisted Solution

by:Lord_Death
Lord_Death earned 150 total points
ID: 36545180
<!DOCTYPE HTML>
<html>
<script type="text/javascript">

    function MyScript()
    {
        var MyHTML = "<h3>This is a Sample</h3>";

        document.getElementById('MySpan').innerHTML = MyHTML;
        //Line Above Or Below no difference ;)
        MySpan.innerHTML = MyHTML;
    }  
</script>

<body onload="MyScript()">
    <span id="MySpan">
    </span>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 36545215
<html>
<head>
script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function MyMessage() {
   return "Hello";
}

window.onload = function() {
    $("#you_div_or_span_ID").html( MyMessage() );
}
</script>
<body>
<span id="you_div_or_span_ID"> ??? </span>
</body>
</html>

Open in new window

0
 

Author Comment

by:newbieweb
ID: 36546826
leakim971, yours loks easiest to me. But, the creation of the return string is not known at load time. I'd like to call the function to initialize the function, when the dialog is being created.

It was suggested by a friend that in my jQuery function I initalize the span and "drill down" as needed.

It's got an ul and an li which I will set to have an empty value. Then I wil fill it from inside my function.

It's be great if youi had the code for that.


Thanks,
newbieweb
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36546837
>But, the creation of the return string is not known at load time.

this is not a problem
line 10 we call a function, we don't know anything about the string until the function return it.

so all the job to be done is on this function : MyMessage

>when the dialog is being created.

so replace window.onload  by the name of the callback parameter of your dialog

not sure where we're going, I think you need to split your question
0
 

Author Closing Comment

by:newbieweb
ID: 36546840
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question