[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how can I display Div value?

Posted on 2012-08-22
10
Medium Priority
?
191 Views
Last Modified: 2012-10-02
<div id='Div1'>alert test</div>  - this is in my code

How can I display "alert test"


alert(document.getElementById("Div1").innerHTML); ??? is not working
0
Comment
Question by:goodk
  • 5
  • 4
10 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38321549
The line as it stands should work, but with jus one line on code I will have to guess that you are trying to execute it before the page is fully loaded.  If that is the case you need to use the the onload event:
<body onload='alert(document.getElementById("Div1").innerHTML);'>

Cd&
0
 
LVL 10

Expert Comment

by:tdlewis
ID: 38321683
To put what COBOLdinosaur said another way, you need to ensure the div is rendered before the code executes. Two other ways to accomplish are:
<div id='Div1'>alert test</div>
<script>alert(document.getElementById("Div1").innerHTML)</script>

Open in new window


and
<head>
<script>
function onLoad()
{
  alert(document.getElementById("Div1").innerHTML);
}
</script>
</head>
<body onload="onLoad();">
<div id='Div1'>alert test</div>
</body>

Open in new window

0
 

Author Comment

by:goodk
ID: 38323422
I initialize it like this,

 xValue = xValue + " <li id='Li1" + bID + "'> ";
            xValue = xValue + " <div id='Div1' onclick=\"showHint(this.value,'changetoinput'," + xDBItem + ") \"> ";

            xValue = xValue + xFieldValue;
            xValue = xValue + "  </div> ";
            xValue = xValue + " </li> ";

this is how I am trying to display

var bID = xTable + xField + xID + xIDValue;  // unique identifier
         alert(document.getElementById("Div1").innerHTML);
         alert(document.getElementById("Li1" + bID).innerHTML);

but this is not working
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:tdlewis
ID: 38323654
xValue is just a string until you add it into the body of the document and once you add it then your alert should work. What do you do with xValue after you construct the string and before the alert statement is executed?
0
 

Author Comment

by:goodk
ID: 38324900
The first section is written out with Response.write(xValue)

and the second section is in the client side script

In general, should
alert(document.getElementById("Div1").innerHTML);
show  xFieldValue?

thanks
0
 
LVL 10

Expert Comment

by:tdlewis
ID: 38325772
If the HTML that is contained in xValue has been rendered in the browser then I would expect the alert to show xFieldValue. You must determine when the alert is executed relative to the rendering of the page. Consider this page:
<html>
<head>
<script>
alert(document.getElementById("Div1").innerHTML);
</script>
</head>
<body>
<div id='Div1'>alert test</div>
</body>
</html>

Open in new window

The alert is executed while the <head> section is being processed. The <body> has not even been seen by the browser yet and there is no "Div1" for getElementById to retrieve.

In general, the most reliable way to access body content is to wait for the entire body to be rendered by the browser, which means adding an onLoad attribute to your <body> tag.
0
 

Author Comment

by:goodk
ID: 38327530
Oh, but the response.write part is first executed and the page is displayed

then the onclick part executed when the user click it on the client side.  So now I expect the content of Div1 to be displayed in an alert message

Realize <div is wrapped inside the <li so I am wondering does that make a difference when the value is accessed?
0
 

Author Comment

by:goodk
ID: 38329810
alert(document.getElementById("Div1").innerHTML);
 alert(document.getElementById("Li1" + bID).innerHTML);

The div id=Div1 alert is showing the word - fieldvalue but li id= Li1+bID is showing up the entire value as

<div id='Div1' >xFieldValue</div>

How do I display ony xFieldValue?
0
 
LVL 10

Accepted Solution

by:
tdlewis earned 2000 total points
ID: 38330190
I created the HTML fragment that appears below. When I execute
    alert(document.getElementById("Div1").innerHTML);
I get "xFieldValue1". Can you please post a fragment of the final HTML that is rendered in the browser.
<ul>
  <li id='Li11'>
    <div id='Div1' onclick="showHint(this.value,'changetoinput',xDBItem)">xFieldValue1</div>
  </li>
</ul>

Open in new window

0
 

Author Closing Comment

by:goodk
ID: 38455695
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 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