Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts

    Code to clear a text field not working

    So sorry. I posted in the wrong forum. This should be in the Javascript forum.

    Hi All,

    I have a function that I have been working on to dynamically (based on the user's selection in a drop list) enable and destroy spry validation for a couple of text fields (while also showing or hiding these fields). This part is working great but what I would like to do is if the user fills in data for these fields and then changes their mind and makes another selection I would like the fields to be cleared so that the data will not be entered into my database. I have what I think should be a working code but for some reason it's not. Could someone help me figure out what I am overlooking here? I added "this is not working" to my comments where the broken code is.

    Thanks in advance,

    Ken

    Code:
    // show and hide sections of a form
    function preparePage() {
    
      var EquipLoc = document.getElementById("EquipLoc");
      var EquipID = document.getElementById("EquipID");
    	
      document.getElementById("List1").onclick = function() {
    		if (document.getElementById("List1").value=="Equipment issue") {
    			// use CSS style to show it
    			document.getElementById("Equipment").style.display = "block";
    			document.getElementById("Satellites").style.display = "none";
    
          // if there isn't a validaton, build one
          if(!sprytextfield2){
          sprytextfield2 = new Spry.Widget.ValidationTextField("spryEquipID", "none", {validateOn:["blur"]});
          }
          if(!spryselect1){
          spryselect1 = new Spry.Widget.ValidationSelect("spryEquipLoc", {validateOn:["blur"]});
          } 
    
    		} else if (document.getElementById("List1").value=="Satellite issue") {
    			// use CSS style to show it
    			document.getElementById("Satellites").style.display = "block";
    			document.getElementById("Equipment").style.display = "none";
    
          //if exists, destroy spry field validation for these fields
          if (sprytextfield2) {
          sprytextfield2.resetClasses();
          sprytextfield2.destroy();
          sprytextfield2 = null;
          }
    
          if (spryselect1) {
          spryselect1.resetClasses();
          spryselect1.destroy();
          spryselect1 = null;
          } 
     
          //clear any values from these fields (this isn't working)
          if (null !== EquipLoc){
          EquipLoc.value="";
          }
    
          if (null !== EquipID){
          EquipID.value="";
          }
    		} else {
    			// hide the div
    			document.getElementById("Equipment").style.display = "none";
    			document.getElementById("Satellites").style.display = "none";
    
         //if exists, destroy spry field validation for these fields
          if (sprytextfield2) {
          sprytextfield2.resetClasses();
          sprytextfield2.destroy();
          sprytextfield2 = null;
          }
    
          if (spryselect1) {
          spryselect1.resetClasses();
          spryselect1.destroy();
          spryselect1 = null;
          } 
    
          //clear any values from these fields (this isn't working)
          if (null !== EquipLoc){
          EquipLoc.value="";
          }
    
          if (null !== EquipID){
          EquipID.value="";
          }
        }  
    };
    Last edited by MaDmiX; 05-28-2012 at 02:55 AM. Reason: Alert I posted in the wrong forum.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,215
    Thanks
    23
    Thanked 605 Times in 604 Posts
    maybe try this
    Code:
    			
    if (EquipID.value != "")
    {
       EquipID.value="";
    }
    instead of this
    Code:
    			
    if (null !== EquipID)
    {
       EquipID.value="";
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    A form field can never be null. Only "" (blank).

    But
    if (EquipID.value != "") {
    EquipID.value="";
    }

    can be reduced to simply:-

    EquipID.value="";

    Which deletes the field content regardless of whatever (if anything) is in it. Is that what you want?
    Last edited by Philip M; 05-28-2012 at 04:08 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    190
    Thanks
    50
    Thanked 0 Times in 0 Posts
    Thanks guys. Still working on it. I will post back with any progress and working code.

    Ken


  •  

    Posting Permissions

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