Results 1 to 6 of 6

Thread: [RESOLVED] Toggle hide/show form element code not working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Resolved [RESOLVED] Toggle hide/show form element code not working

    I've taken some code from here:

    http://www.webwinnerdesigns.com/js-s...orm.html#myrow

    To help show/hide a form element. What I have is a link and clicking it will open another form containing a text input and button:

    Code:
    <form id="form" name="form" action="<?php echo $PHP_SELF;?>" method="post">
    <label>Email</label><input type="text" name="email" id="email" />
    <label>Password</label><input type="password" name="password" id="password" />
    <input name="login" value="Login" type="submit"><br />
    <a href="#forgotten" onClick="toggle_it('forgotten')">Cant remember</a> | <a href="index.php#register">Register</a>
    <form id="forgotten">
    <label style="display:none;">Email</label><input type="text" name="forgotten" style="display:none;" />
    <input name="remind" value="Remind" type="submit" style="display:none;"><br />
    </form>
    So as you can see, there are 2 forms. The first form has a link 'cant remember' which on click will open form 2, thus allowing the user to submit their email for forgotten password link.

    At the moment the show/hide is just not working. I've tried adding 'id=forgotten' to each element in the form, to only one element, to just the form - nothing is working.

    How can I fix this??

    By the way, the toggle script is:

    Code:
    <script language="javascript">
    function toggle_it(itemID){
    // Toggle visibility between none and inline
    if ((document.getElementById(itemID).style.display == 'none'))
    {
    document.getElementById(itemID).style.display = '';
    } else {
    document.getElementById(itemID).style.display = 'none';
    }
    }
    </script>
    Any help would be awesome - thanks!

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Toggle hide/show form element code not working

    Try this (code taken from post #1):

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="javascript">
    function toggle_it(itemID){
    // Toggle visibility between none and inline
    if ((document.getElementById(itemID).style.display == 'none'))
    {
    document.getElementById(itemID).style.display = '';
    } else {
    document.getElementById(itemID).style.display = 'none';
    }
    }
    </script>
    
    <style type="text/css">
    <!--
    .style5 {font-family: Arial, Helvetica, sans-serif; font-size: 10; }
    .style7 {font-family: Arial, Helvetica, sans-serif; font-size: 10px; }
    -->
    </style> 
    </head>
    <body>
    <form id="form" name="form" action="<?php echo $PHP_SELF;?>" method="post">
    <label>Email</label><input type="text" name="email" id="email" />
    <label>Password</label><input type="password" name="password" id="password" />
    <input name="login" value="Login" type="submit"><br />
    <a href="#forgotten" onClick="toggle_it('forgotten')">Cant remember</a> | <a href="index.php#register">Register</a>
    <form id="forgotten">
    <label style="display:none;">Email</label><input type="text" name="forgotten" style="display:none;" />
    <input name="remind" value="Remind" type="submit" style="display:none;"><br />
    </form>
    </div>
    </body>
    </html>
    Last edited by Nightwalker83; Dec 1st, 2008 at 02:26 AM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Toggle hide/show form element code not working

    It just doesn't do anything - I checked the error console and the error I'm getting is:

    Code:
    document.getElementById(itemID) is null
    Any thoughts on why this is? I'm guessing the form cannot be passed this way or something like that?

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Toggle hide/show form element code not working

    Quote Originally Posted by wwwfilmfilercom
    Any thoughts on why this is? I'm guessing the form cannot be passed this way or something like that?
    Yeah, that might be the case! I tried wrapping the form in a table and it still did not work.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Toggle hide/show form element code not working

    I would place the items inside a div and then show or hide that.
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script language="javascript">
    function toggle_it(itemID){
    // Toggle visibility between none and inline
    	if ((document.getElementById(itemID).style.display == 'none'))
    		{
    		document.getElementById(itemID).style.display = '';
    		} 
    	else 
    		{
    		document.getElementById(itemID).style.display = 'none';
    		}
    	}
    </script>
    </head>
    <body>
    	<form id="form" name="form" action="<?php echo $PHP_SELF;?>" method="post">
    		<label>Email</label><input type="text" name="email" id="email" />
    		<label>Password</label><input type="password" name="password" id="password" />
    		<input name="login" value="Login" type="submit" ID="Submit1"><br />
    		<a href="#forgotten" onClick="toggle_it('forgotten')">Cant remember</a> | <a href="index.php#register">Register</a>
    		
    		<div id='forgotten' style="display:none;">
    			<label>Email</label><input type="text" name="forgotten"  ID="Text1"/>
    			<input name="remind" value="Remind" type="submit"  ID="Submit2"><br />
    		</div>
    	</form>
    </body>
    </html>

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Toggle hide/show form element code not working

    Thanks! That works just great

Posting Permissions

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



Click Here to Expand Forum to Full Width