SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ignorant wannabe baffled - easy for you though

    hello all, could really use a point in the right direction here PLEASE:

    am showing or hiding a field in form depending upon a value in another field. this code works great, EXCEPT for first time through script.

    <script type="text/javascript">
    function writebox(form) {
    if (form.repeat.value=='0') {
    document.getElementById("dDateE").style.visibility = "hidden";
    } else {
    document.getElementById("dDateE").style.visibility = "visible";
    }
    }

    so in addition I added this bit for onload, but it does not seem to be working, but how can it not be working? I mean it's identical right?

    window.onload = function writebox(form) {
    if (form.repeat.value=='0') {
    document.getElementById("dDateE").style.visibility = "hidden";
    } else {
    document.getElementById("dDateE").style.visibility = "visible";
    }
    }

    the first function is on top of page and that onload bit is down in a part of the script I know is executed first time through.

    I'm a newbie and lost and would really just a push in the right direction here!

    many thanks,

    mike
    Last edited by m300zx; Apr 20, 2011 at 10:55. Reason: incomplete

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    The problem is that the form value is not passed to it.

    If you put your script at the bottom of the body (just before the </body> tag) you can leave the function as you started, and then call that function with a reference to the form.

    Code html4strict:
    <form id="descriptiveIdentifierForForm" ...>

    Code javascript:
    function writebox(form) {
        ...
    }
     
    var form = document.getElementById('descriptiveIdentifierForForm');
    writebox(form);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    many thanks Paul !!

    sorry about the late reply... forgot about my post here when I discovered that hiding that field solved one problem but caused another. I was better off sending a warning message and not accepting the value in the field. but thanks for your time and knowledge... I'm sure your reply will come in handy somewhere down the road...

    hope Christchurch is doing well now.


Bookmarks

Posting Permissions

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