...

View Full Version : Code to clear a text field not working



MaDmiX
05-28-2012, 03:08 AM
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


// 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="";
}
}
};

sunfighter
05-28-2012, 03:31 PM
maybe try this


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

instead of this


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

Philip M
05-28-2012, 05:02 PM
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?

MaDmiX
05-30-2012, 05:16 AM
Thanks guys. Still working on it. I will post back with any progress and working code.

Ken



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum