...

View Full Version : Values from a div /div field?



bvtoffin
06-29-2004, 10:49 PM
I am having problems with getting a value into a variable from a hidden <div> field - all I get is 'undefined'.

The content for the <div> field comes from a tag which is automatically changed in the page as it loads.

Simplified version of the page is as follows;

<head>
<script language="javascript">

var info1=document.all.refnc.innerhtml;
var info=(info1.substring(58,64));

</script>
</head>

<body>

<script>document.write(itin_info);</script>

<div id="refnc" style="display:none">
<!-- OD_INSERT Reference -->
</div>

</body>

As the page is built the " <!-- OD_INSERT Reference --> " gets translated to the following;

<div id="refnc" style="display:none">
<font size="3">Reference</font>
<font size="4"> GH/123456</font>

</div>

Note: the line break at the end of both lines.


Any help with this would appreciated.

Cheers

BVT.

sad69
06-30-2004, 12:05 AM
Well you can do one of two things:
1. dbl-check your code
2. use DOM

1. take a look at the alert generated and start counting characters


<head>
<script language="javascript">

var info1=document.all.refnc.innerhtml;
alert(info1);
var info=(info1.substring(58,64));

</script>
</head>


2. using DOM:


var refnc = document.getElementById('refnc');
var info = refnc.childNodes.item(1).firstChild;


That might not work as the index 1 may be incorrect as you mentioned line breaks... it may also differ from browser to browser (although it looks like you only care about IE - document.all....), but I'm pretty sure that should do it.

To help with debugging:


var refnc = document.getElementById('refnc');
alert(refnc.childNodes.length); //num children, hopefully == 2
for(var i = 0; i < refnc.childNodes.length; i++) {
alert(refnc.childNodes.item(i).nodeName+' '+i);
}
var info = refnc.childNodes.item(1).firstChild;


I'm guessing you want the second FONT node and the corresponding index.

Hopefully that makes sense. If you're still having trouble, post back.

Good luck,
Sadiq.

Willy Duitt
06-30-2004, 12:14 AM
It's innerHTML not innerhtml

And move your script block from the head to immediately before your closing body tag. Where it is, the division is not yet built before the script is parsed. (or you could place in a function and call body onload)

<script language="javascript">

var info1=document.all.refnc.innerHTML;
var info=(info1.substring(58,64));

</script>
</body>

bvtoffin
06-30-2004, 02:46 AM
Thnaks Guys - ended up moving the script (as advised) and changing to;

var info1=document.getElementById("refnc").innerText;
var info=(info1.substring(21,27));


Appreciate the help!!
:thumbsup:

glenngv
06-30-2004, 05:37 AM
Take note that if you plan to make it cross-browser in the future, your current code will not work because innerText is an IE-proprietary property.

bvtoffin
07-01-2004, 06:07 AM
Thanks glenngv - didn't realise that - any suggestions what to change it to??

Cheers

glenngv
07-01-2004, 09:19 AM
Try using DOM as suggested by sad69.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum