• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 103
  • Last Modified:

how to get to the div value?

This alert(document.getElementById("Li1").innerHTML);


 shows                    <div id="divid">myName</div>

I also want to do                 alert(?????) to display             myName

how do i do that?

thanks
0
goodk
Asked:
goodk
  • 5
  • 2
6 Solutions
 
Jeff DarlingDeveloper AnalystCommented:
alert(document.getElementById("divid").innerHTML); 

Open in new window

0
 
Jeff DarlingDeveloper AnalystCommented:
sample
<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>
  <script>
  function Getdivid(){
   alert(document.getElementById("divid").innerHTML);
  }
  function Getli1(){
   alert(document.getElementById("li1").innerHTML);
  }
  </script>
</head>

<body>
<div id="li1">
<div id="divid">myName</div>
</div>
<input type="button" onclick="Getdivid()" value="Get divid" />
<input type="button" onclick="Getli1()" value="Get li1" />
</body>

</html>

Open in new window

0
 
goodkAuthor Commented:
May be I did not explain,  the div is inside a div to make it unique as I have several on the page.


<div id="outerID_1">  <div id="innerID">thisname</div></div>

how to alert(  outerID_1.innerID  )  ????? to display thisname
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Jeff DarlingDeveloper AnalystCommented:
The way I would do it would be to create a document and populate that fragment into the document.

Now, here is a function,  that displays the name inside the li1div

<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>
  <script>
  function Getdivid(myli){
    var tempDoc = document.implementation.createHTMLDocument('');
    tempDoc.write(document.getElementById(myli).innerHTML);
    alert(tempDoc.getElementById("divid").innerHTML);
  }

  </script>
</head>

<body>
<div id="li1">
<div id="divid">John</div>
</div>
<div id="li2">
<div id="divid">Sara</div>
</div>

<input type="button" onclick="Getdivid('li1')" value="Get divid" />

</body>

</html>

Open in new window

0
 
Jeff DarlingDeveloper AnalystCommented:
Here is a version that works in IE9.  IE9 is a bit picky..

<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=9" />
  <title>Untitled</title>
  <script>
  function Getdivid(myli){
    var tempDoc = document.implementation.createHTMLDocument("New Document");
    tempDoc.body.innerHTML =  document.getElementById(myli).innerHTML
    alert(tempDoc.getElementById("divid").innerHTML);
  }

  </script>
</head>

<body>
<div id="li1">
<div id="divid">John</div>
</div>
<div id="li2">
<div id="divid">Sara</div>
</div>

<input type="button" onclick="Getdivid('li1')" value="Get divid li1" />
<input type="button" onclick="Getdivid('li2')" value="Get divid li2" />

</body>

</html>

Open in new window

0
 
Rainer JeschorCommented:
Hi,

how about the function "textContent" ?

document.getElementById("divid").textContent;

Open in new window


Sample:
http://jsfiddle.net/EE_RainerJ/Ljpq3hwq/

HTH
Rainer
0
 
Jeff DarlingDeveloper AnalystCommented:
There is more than one div with the id divid.

another way would be to use jquery.

alert($('#li1').first('#divid').text());

Open in new window

0
 
goodkAuthor Commented:
thank you guys for your help
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.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now