SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What's wrong with this function?

    Code:
    <script>
    function Affiliate()
    {
    if (document.form1.affiliate.value=='1') {
    	document.form1.url.disabled=false;
    	}
    	else 
    		{document.form1.url.disabled=true;
    		}
    }
    </script>
    I wrote the above script to toggle a textfield named "url" between enabled and disabled. The toggle is controlled by two radio buttons named "affiliate" one with value="0" the other with value="1" and onchange handlers calling the above function.

    Yet I run the script, nothing happens, no errors no actions...
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's advisable to access elements using the getElementById method, also, for what you require, your probably better off using a checkbox instead of radio buttons.

    Have a look at my example.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function toggleUrl(bool) {
                    var url = document.getElementById('affiliate');
                        url.disabled = !bool;
                }
            </script>
        </head>
        <body>
            
            <label for="txtUrl">Url: </label><input type="text" id="affiliate" disabled /><br />
            <label for="chkIsAffiliate">Affiliate: </label><input type="checkbox" id="chkIsAffiliate" onclick="toggleUrl(this.checked);" />
    
        </body>
    </html>


  3. #3
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already have a working function which uses checkboxes, but I specifically wanted to use radio buttons in this example.
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ammended

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                function toggleUrl(w) {
                    var url = document.getElementById('affiliate');
                        url.disabled = (w.value == 0) ? true : false;
                }
            </script>
        </head>
        <body>
            
            <label for="txtUrl">Url: </label><input type="text" id="affiliate" disabled /><br />
            <label>Affiliate: </label><label for="chkIsAffiliate">Yes </label><input type="radio" name="chkIsAffiliate" id="chkIsAffiliate" value="1" onclick="toggleUrl(this)"/><label for="chkIsAffiliate">No </label><input type="radio" name="chkIsAffiliate" id="chkIsAffiliate" value="0" onclick="toggleUrl(this)" />
    
        </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
  •