Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting value from a DIV tag

Status
Not open for further replies.

Funkar

Programmer
Oct 16, 2006
8
NL
Hello all,

i am new to JavaScript, I am trying to get a div tag value using javascript. i have a div tag in html like this

<div id="para1"> </div>


there is another javascript that do some calculation and sets the result to para1 tag to display.

now i want to use this value somewhere else, I am trying

var temp = document.getElementById("para1");
var save_temp = temp.innerHTML;

but variable save_temp always get an empty value. i have no idea what to do, any help would b really appriciated.

thanks
 
What code do you use in the other call to set the data into para1? There's nothing obviously wrong with your reasoning thus far, but you've missed out half the code needed to answer you ;-)
 
I am using this javascript function in php to copy a DIV value from inside the inframe to the DIV in parent window.

<?php
echo "<script language=\"JavaScript\">\n";
echo "function test() {";
echo "var y=document.getElementById('score').cloneNode(true); ";
echo "parent.document.getElementById('para1').appendChild(y); ";
echo "alert(\"value copied!\");\n";
echo "}";
echo "</script>";
?>

the score DIV tag is

echo "<div id=\"score\">".$display_r."</div>";
 
Then you'll not find the value in the HTML methods; the data was never placed there. You'll need to use something like:
Code:
parent.document.getElementById('para1').lastChild
to get the last node added to para1; dependending on what type of node you've cloned will determine how you need to access it to get the value you want. If it's just text, that'll do on its own.
 
i checked from the DOM inspector the para1 div tag contains another div tag after cloned. the dom tree is like

-DIV
-DIV
-NAME
-#text (<-- this value is what i need to extract)

i tried the way you said, it returns [object HTMLDivElement]. i think i am missing something here.

 
Why not try this, do you mean within the iframe?
[tt] parent.document.getElementById('score').innerHTML[/tt]
 
no, i am accessing it in the parent window.

Code:
var t = document.getElementById('para1').lastChild
var p_value = t.innerHTML;

this code returns the desired string but with the HTML tags

Code:
<result> value </result>

how can i just get the VALUE without the HTML tags?
 
From the parent itself, should it be just this???
[tt] document.getElementById('score').innerHTML[/tt]
 
the 'score' div tag is in the iframe, this div tag is cloned and appended to the parent div tag 'para1' as a child. so the tree in the parent window is

Code:
-DIV (para1)
 -DIV (score)
  -NAME
    #text

i hope it is clear now.
 
If score is the id, why do you have to care about the id para1? The getElementById() is meant to, in a sense, flatten the hierarchical structure.
 
yes you are right!

but the question remains. even i am accessing 'socre' i am still getting desired string in html tags by using innerHTML. i have tried different combination with getElementById and getElementByTagName but nothing works for me. i dont know what i am missing.
 
finally solved.

Code:
var t = document.getElementsByTagName("name");
var v_value = abc[0].firstChild.nodeValue;

thank tsuji and MOrac.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top