SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple javascript question ... i think

    In the following code:
    Code:
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    
    <!--
    
    function disableelements(formname,x) 
    
    {
    
    	if (formname.condition(x).disabled == false)
    
    	  {
    
    		eval("formname.condition"+x+".disabled=true;");
    	  }
    	else if (formname.condition(x).disabled == true)
    	  {
    		eval("formname.condition"+x+".disabled=false;");
    	  }
    }
    //-->
    </SCRIPT>
    It will work if I hard code the value of x into the if statement (ex: formname.condition1). However in this sample code it will not work if I try to add the variable x into the if statement. How do I get it to use the x variable without hard coding that information in?

  2. #2
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using:

    Code:
    document.getElementById("condition" + x").disabled = true;
    The getElementById works great for dynamic stuff.

    You can also do this:

    Code:
    formname.elements["condition" + x].disabled = true;

  3. #3
    Procrastinator Extraordinaire lucas's Avatar
    Join Date
    Oct 2000
    Location
    Springfield, Missouri, U.S.A.
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thankya.

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm presuming you are trying to switch the disable flag. I'm also presuming you are sending the 'form object' and not the 'form name'

    Code:
    function disableEls(formObj,x)
    {
      var fldObj = formObj.elements['condition' + x];
    fldObj.disabled = (fldObj.disabled == false) ? true : false;
    }


    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •