Hide/Unhide form fields

Discussion in 'JavaScript' started by jcnkty, Jul 11, 2007.

  1. #1
    Hi guys this is my first post, just want to ask how to make a form page that hide/unhide form fields using a single checkbox.

    thanks!
     
    jcnkty, Jul 11, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <input type="checkbox" onclick="showfield(this.checked, 'id-of-field');" />
    
    <input type="text" id="id-of-field" style="display: none;" />
    
    
    HTML:
    
    function showfield(checked, id)
    {
        document.getElementById(id).style.display = checked ? 'block' : 'none';
    }
    
    Code (javascript):
     
    nico_swd, Jul 11, 2007 IP
  3. jcnkty

    jcnkty Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for your reply.., how do i use the code in multiple form items to hide/unhide?
     
    jcnkty, Jul 11, 2007 IP
  4. SEV3N

    SEV3N Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use
    document.getElementsByTagName('<tag>')
    Code (markup):
    You should use a loop (foreach, for or while) to change every item's display style.
     
    SEV3N, Jul 12, 2007 IP
  5. jcnkty

    jcnkty Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    how? sorry just a beginner..
     
    jcnkty, Jul 12, 2007 IP
  6. SEV3N

    SEV3N Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    function findimg()
    {
     var imgs,i;
     imgs=document.getElementsByTagName('img');
     for(i=0;i<imgs.length;i++)
     {
       alert(imgs[i].id);
     }
    }
    
    Code (markup):
    This code reads every <img> tag (image) and alerts it's id.
    You should use something similar. Adapt something a similar script to your needs.
     
    SEV3N, Jul 14, 2007 IP