Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Values from a div /div field?

    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.
    BVT.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    <head>
    <script language="javascript">
    
    var info1=document.all.refnc.innerhtml;
    alert(info1);
    var info=(info1.substring(58,64));
    
    </script>
    </head>
    2. using DOM:
    Code:
     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:
    Code:
     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.

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #4
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!!
    BVT.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks glenngv - didn't realise that - any suggestions what to change it to??

    Cheers
    BVT.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Try using DOM as suggested by sad69.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •