SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    ny
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript - Clear Values

    Hi all.
    I need to clear the value of some check boxes,select boxes, and text boxes by pressing a button called "clear off"..
    Any help will be appreciated...I dont want a reset button because in the edit mode of application I am re-populating the form field values....

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not exactly what you asked, but close enough to give you the idea of what to do. (This was a test file I created for someone else's question here a couple fo weeks ago).
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function clearchecks (el) {
    	var grp = el.name;
    	var frm = document.getElementById('groups');
    	for (i=0; i<frm.length; i++) {
    		var item = frm.elements[i];
    		if (item.name.substr(0,grp.length) == grp && item.name.length == 8) {
    			if (el.checked) {
    				// do nothing
    			} else {
    				switch (item.type) {
    					case 'checkbox':
    						item.checked = false;
    						break;
    					case 'radio':
    						item.checked = false;
    						break;
    					case 'select':
    						item.selectedIndex = 0;
    						break;
    					default:
    						item.value = '';
    				}
    			}
    		}
    	}
    }
    </script>
    </head>
    <body>
    <form id="groups" onsubmit="return false">
    <input type="checkbox" name="group1" onclick="clearchecks(this)" /> - Group 1<br />
    1 - <input type="checkbox" name="group1_1" />
     - 2 - <input type="checkbox" name="group1_2" />
     - 3 - <input type="checkbox" name="group1_3" />
     - 4 - <input type="checkbox" name="group1_4" />
     - 5 - <input type="text" name="group1_5" />
     - 6 - <textarea name="group1_6"></textarea>
     - 7 - <select name="group1_7" />
    <option value="#">-None-</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D">D</option>
    </select>
    </form>
    </body>
    </html>
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    ny
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is very helpfull...thank u..

  4. #4
    Non-Member
    Join Date
    Aug 2007
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can call reset method to do this, document.form_name.reset();


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
  •