#1
  1. No Profile Picture
    The one who gets it done
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Madison, WI
    Posts
    395
    Rep Power
    14

    Question Is there an 'exists' function in JavaScript?


    The explanation: I'm combining ASP and JavaScript. For those of you unfamiliar with ASP (as I am with PHP...), ASP basically outputs all the HTML for a page to the browser, including the JS.

    Now, I have a membership application that can be used for 3 different associations, WTCA, STCA and SCDA. They have different dues structures, so that while WTCA has associate members, STCA does not, etc.

    When I created this page only for WTCA membership, I wrote some nice JS that when you select one type of membership (radio button), it disables the controls in the other types.

    The problem: There are 5 different membership types between all associations, but only 3 (at most) appear at any one time. If I write the JS to change all controls, some of them don't exist on the page, so the script doesn't run.

    The question: Is there some sort of 'if exists' function I can use so the JS can check to see if something is there before trying to disable it? I want to avoid having to deal with changing the JS with ASP while creating the page.

    Your help is appreciated.

    Jay
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Wilmslow, Cheshire
    Posts
    255
    Rep Power
    18
    To save length of script being sent to the client, and therefore file size - and risk of errors - I would say it's better to create your JavaScript function in the ASP page and only send the relevent code to the user.

    But, the alternative (for finding out if a form element, or anything else, exists) is to just say:

    if document.forms.xxx.xxx { do something }
    else { do something else }

    Here's an example:

    <HTML>
    <HEAD>
    <TITLE>Cheese Demonstration</TITLE>

    <SCRIPT LANGUAGE="JavaScript">
    function CheckForm(formname){
    if (document.forms[formname].cheese) {
    alert("Cheese exists in this form and says: '" + document.forms[formname].cheese.value + "'");
    }
    else {
    alert("Sadly, cheese doesn't exist in this form");
    }
    }
    </SCRIPT>

    </HEAD>
    <BODY>

    FORM 1: NO CHEESE<P>

    <FORM NAME="Form1" onsubmit="CheckForm('Form1')">
    <INPUT TYPE="HIDDEN" NAME="noCheese" VALUE="No Cheese">
    <INPUT TYPE="SUBMIT" VALUE="No cheese in this form">
    </FORM><P>

    FORM 2: CHEESE<P>
    <FORM NAME="Form2" onsubmit="CheckForm('Form2')">
    <INPUT TYPE="HIDDEN" NAME="cheese" VALUE="Good day to you sir">
    <INPUT TYPE="SUBMIT" VALUE="Cheese is in this form">
    </FORM>

    </BODY>
    </HTML>
  4. #3
  5. No Profile Picture
    The one who gets it done
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Madison, WI
    Posts
    395
    Rep Power
    14

    Talking Exactly!


    Thanks, Dang. This is exactly what I needed. You can probably tell I'm a JS newbie - simple, yet elegant.

    Your point is well taken about using ASP to send only relevant code. However, I'm on a time constraint, and the number of lines of code would balloon if I did it this way.

    Thanks so much for your help.

    Jay
  6. #4
  7. No Profile Picture
    The one who gets it done
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Madison, WI
    Posts
    395
    Rep Power
    14

    Cool The solution


    Here's the final code I'm using. As you can see, there are four member types and six total controls. Thanks again for your help, Dang! Another unexpected perk: when combo boxes are set to "" they are blank. When I enable them and set them to 0, the 'select from the list' item (first, value=0) appears again. Very cool!

    Jay

    Code:
    	//each button passes the btnSelected value...
    	function CheckMemType(btnSelected)
    	{
    		var frm = document.frmAdd	//my form
    		switch(btnSelected)
    		{
    		
    			case 1:		//Regular
    				frm.DuesLevelReg.disabled = false;
    				frm.DuesLevelReg.value = 0;
    				if (frm.ProductsServices) {
    					frm.ProductsServices.value = "";
    					frm.ProductsServices.disabled = true;}
    				if (frm.CoTypeSup) {
    					frm.CoTypeSup.value = "";
    					frm.CoTypeSup.disabled = true;}
    				if (frm.DuesLevelSup) {
    					frm.DuesLevelSup.value = "";
    					frm.DuesLevelSup.disabled = true;}
    				if (frm.CoTypeAss) {
    					frm.CoTypeAss.value = "";
    					frm.CoTypeAss.disabled = true;}
    				if (frm.ProfMemPack) {
    					frm.ProfMemPack.checked = false;
    					frm.ProfMemPack.disabled = true;}
    			break;
    			case 2:		//Supplier
    				if (frm.DuesLevelReg) {
    					frm.DuesLevelReg.value = "";
    					frm.DuesLevelReg.disabled = true;}
    				if (frm.ProductsServices) {
    					frm.ProductsServices.disabled = false;}
    				if (frm.CoTypeSup) {
    					frm.CoTypeSup.disabled = false;
    					frm.CoTypeSup.value = 0;}
    				if (frm.DuesLevelSup) {
    					frm.DuesLevelSup.disabled = false;
    					frm.DuesLevelSup.value = 0;}
    				if (frm.CoTypeAss) {
    					frm.CoTypeAss.value = "";
    					frm.CoTypeAss.disabled = true;}
    				if (frm.ProfMemPack) {
    					frm.ProfMemPack.checked = false;
    					frm.ProfMemPack.disabled = true;}
    			break;
    			case 3:		//Associate
    				if (frm.DuesLevelReg) {
    					frm.DuesLevelReg.value = "";
    					frm.DuesLevelReg.disabled = true;}
    				if (frm.ProductsServices) {
    					frm.ProductsServices.value = "";
    					frm.ProductsServices.disabled = true;}
    				if (frm.CoTypeSup) {
    					frm.CoTypeSup.value = "";
    					frm.CoTypeSup.disabled = true;}
    				if (frm.DuesLevelSup) {
    					frm.DuesLevelSup.value = "";
    					frm.DuesLevelSup.disabled = true;}
    				frm.CoTypeAss.disabled = false;
    				frm.CoTypeAss.value = 0;
    				if (frm.ProfMemPack) {
    					frm.ProfMemPack.checked = false;
    					frm.ProfMemPack.disabled = true;}
    			break;
    			case 4:		//Professional
    				if (frm.DuesLevelReg) {
    					frm.DuesLevelReg.value = "";
    					frm.DuesLevelReg.disabled = true;}
    				if (frm.ProductsServices) {
    					frm.ProductsServices.value = "";
    					frm.ProductsServices.disabled = true;}
    				if (frm.CoTypeSup) {
    					frm.CoTypeSup.value = "";
    					frm.CoTypeSup.disabled = true;}
    				if (frm.DuesLevelSup) {
    					frm.DuesLevelSup.value = "";
    					frm.DuesLevelSup.disabled = true;}
    				if (frm.CoTypeAss) {
    					frm.CoTypeAss.value = "";
    					frm.CoTypeAss.disabled = true;}
    				frm.ProfMemPack.disabled = false;
    			break;
    		}	
    	}

IMN logo majestic logo threadwatch logo seochat tools logo