how can I display Div value?

<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
goodkAsked:
Who is Participating?
 
tdlewisConnect With a Mentor Commented:
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
 
COBOLdinosaurCommented:
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
 
tdlewisCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
goodkAuthor Commented:
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
 
tdlewisCommented:
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
 
goodkAuthor Commented:
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
 
tdlewisCommented:
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
 
goodkAuthor Commented:
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
 
goodkAuthor Commented:
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
 
goodkAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.