Results 1 to 4 of 4

Thread: Simple function not working

  1. #1
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple function not working

    Hi, could you help tell me why my simple function doesn't work here? Not well versed in javascript as you can tell.

    Code:
    <script type="text/javascript">
    function gts_part(id1,id2,id3){
      if (document.getElementById(id1).checked == true)  {
        document.getElementById(id2).disabled = false;
        document.getElementById(id3).disabled = false;}
      else	
        document.getElementById(id2).disabled = true;
        document.getElementById(id3).disabled = true;
       document.getElementById(id2).checked = false;
        document.getElementById(id3).checked = false;	}
    }
    </script>
    
    <input type="checkbox" name="survey" value="gts" id="s1" onclick="gts_part(s1,s2,s3);" /> Commit to Participate<br />
    <input type="radio" name="gts_global" value="global" id="s2" disabled /> Global<br />
    <input type="radio" name="gts_global" value="us" id="s3" disabled /> US Only<br />
    It works ok if I used the individual field ids as in here:

    Code:
    <script type="text/javascript">
    function gss_part(){
      if (document.getElementById("x1").checked == true)  {
        document.getElementById("x2").disabled = false;
        document.getElementById("x3").disabled = false;}
    	
      else{
        document.getElementById("x2").disabled = true;
        document.getElementById("x3").disabled = true;
        document.getElementById("x2").checked = false;
        document.getElementById("x3").checked = false;	
    }}
    </script>
    
    <input type="checkbox" name="survey1" value="gts" id="x1" onclick="gss_part();" /> Commit to Participate<br />
    <input type="radio"  name="gts_global1" value="global" id="x2" disabled /> Global<br />
    <input type="radio"  name="gts_global1" value="us" id="x3" disabled /> US Only<br />

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    function gts_part(id1,id2,id3){
      if (document.getElementById(id1).checked == true)  {
        document.getElementById(id2).disabled = false;
        document.getElementById(id3).disabled = false;
      }
      else {
        document.getElementById(id2).disabled = true;
        document.getElementById(id3).disabled = true;
       document.getElementById(id2).checked = false;
        document.getElementById(id3).checked = false;
       }
    }
    </script>
    
    <input type="checkbox" name="survey" value="gts" id="s1" onclick="gts_part('s1','s2','s3');" /> Commit to Participate<br />
    <input type="radio" name="gts_global" value="global" id="s2" disabled /> Global<br />
    <input type="radio" name="gts_global" value="us" id="s3" disabled /> US Only<br />
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    mtran (11-17-2009)

  4. #3
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default not tried but i think i detected the error

    I'm new here and to js but perhaps this should work if you escaped the ids:

    PHP Code:
    <input type="checkbox" name="survey" value="gts" id="s1" onclick="gts_part('s1','s2','s3');" /> 
    Quote Originally Posted by mtran View Post
    Hi, could you help tell me why my simple function doesn't work here? Not well versed in javascript as you can tell.

    Code:
    <script type="text/javascript">
    function gts_part(id1,id2,id3){
      if (document.getElementById(id1).checked == true)  {
        document.getElementById(id2).disabled = false;
        document.getElementById(id3).disabled = false;}
      else	
        document.getElementById(id2).disabled = true;
        document.getElementById(id3).disabled = true;
       document.getElementById(id2).checked = false;
        document.getElementById(id3).checked = false;	}
    }
    </script>
    
    <input type="checkbox" name="survey" value="gts" id="s1" onclick="gts_part(s1,s2,s3);" /> Commit to Participate<br />
    <input type="radio" name="gts_global" value="global" id="s2" disabled /> Global<br />
    <input type="radio" name="gts_global" value="us" id="s3" disabled /> US Only<br />
    It works ok if I used the individual field ids as in here:

    Code:
    <script type="text/javascript">
    function gss_part(){
      if (document.getElementById("x1").checked == true)  {
        document.getElementById("x2").disabled = false;
        document.getElementById("x3").disabled = false;}
    	
      else{
        document.getElementById("x2").disabled = true;
        document.getElementById("x3").disabled = true;
        document.getElementById("x2").checked = false;
        document.getElementById("x3").checked = false;	
    }}
    </script>
    
    <input type="checkbox" name="survey1" value="gts" id="x1" onclick="gss_part();" /> Commit to Participate<br />
    <input type="radio"  name="gts_global1" value="global" id="x2" disabled /> Global<br />
    <input type="radio"  name="gts_global1" value="us" id="x3" disabled /> US Only<br />

  5. #4
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default sorted!!

    I tested my suggestion and it didn't work, at least in ie. Apparently you canot triger events from inputs, so i would suggest inserting the input in a span element which can trigger events.

    Here's my proposed solution:



    HTML Code:
    <html>
    <!-- Created on: 18/11/2009 -->
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
      <title></title>
      <meta name="description" content="">
      <meta name="keywords" content="">
      <meta name="author" content="Unregistered User">
      <meta name="generator" content="AceHTML Freeware">
    <script type="text/javascript">
    function gts_part(id1, id2, id3){
    
      if (document.getElementById(id1).checked )  {
        document.getElementById(id2).disabled = false;
        document.getElementById(id3).disabled = false;
      }else{	
        document.getElementById(id2).disabled = true;
        document.getElementById(id3).disabled = true;
       document.getElementById(id2).checked = false;
        document.getElementById(id3).checked = false;	
    	}
    
    }
    </script>
    </head>
    <body>
    
    <span onclick="gts_part('s1','s2','s3');">
    <input type="checkbox" name="survey" value="gts" id="s1"/> Commit to Participate<br />
    </span>
    <input type="radio" name="gts_global" value="global" id="s2" disabled /> Global<br />
    <input type="radio" name="gts_global" value="us" id="s3" disabled /> US Only<br />
    </body>
    </html>

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
  •