SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Form help

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form help

    HI Guys

    I am using the code below, which I picked up from this forum to reset a form to
    default.This works wonderfully, problem is I also need to reset the drop down (select) to default as well, please help me alter this code.

    Code:
    <script type="text/javascript">
      function clearForm(form_obj)
    {
      //then we start reseting values to null  
        for (var i = 0; i < form_obj.length; i++) {
            var elem = form_obj.elements[i];
          //we want to clear text and textarea only  
            if (elem.type == 'text' || elem.type == 'textarea' || elem.type == 'password')
               elem.value = '';
               
            if (elem.type == 'radio' || elem.type == 'checkbox')
               elem.checked = false;
        }
    }
    </script>
    Thanks
    Niva

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript">
      function clearForm(form_obj)
    {
      //then we start reseting values to null  
        for (var i = 0; i < form_obj.length; i++) {
            var elem = form_obj.elements[i];
        if (elem.tagName=='SELECT'){
          elem.selectedIndex=0;
        }
          //we want to clear text and textarea only  
            if (elem.type == 'text' || elem.type == 'textarea' || elem.type == 'password')
               elem.value = '';
               
            if (elem.type == 'radio' || elem.type == 'checkbox')
               elem.checked = false;
        }
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you just use the reset button that comes with HTML?
    Or maybe use form.reset()?
    WatermarkIt add watermarks to photos and images
    Salo Storm Software

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Vic

    That work 100%

    Niva

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maro25
    Why don't you just use the reset button that comes with HTML?
    Or maybe use form.reset()?
    It's not so easy when you are using javascript and php sessiions together...

    Thanks for the info though.

    NIva


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
  •