SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

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

    Tickboxes to manipulate Textbox values

    Hi I've written a JavaScript function which changes the value of a form textfield when a tickbox is checked/unchecked.


    It works fine in Firefox (Mozilla) but doesn't work in Internet Explorer... Why not?

    Here's my code:

    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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="JavaScript" type="text/javascript">
    
    function UpdateField()
    {
    if (document.form1.field1.checked==true) {
    	document.form1.field.value=1;
    }
    	else 
    		{
    		document.form1.field.value=0;
    		}
    }
    
    </script>
    </head>
    
    <body>
    <h1>Tickboxes manipulate textboxes</h1>
    <form id="form1" name="form1" method="post" action="">
      <input name="field1" type="checkbox" id="field1" value="1"  onchange="UpdateField()" />
      <input type="text" name="field" value="0" />
      &nbsp;
    </form>
    </body>
    </html>
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  2. #2
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait! Answered my own question!!!

    Change "Onchange" to "onclick"

    Does IE not support OnChange?
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

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

    How would I go about extending this script so it worked for any tickbox/textbox pair with respective names:

    "<fieldname>" & "<fieldname>1"

    (I'm very new to JavaScripting have avoided it like the plague for 6 years - always had someone else to do it!)

    My first thoughts are to parse the tickbox name into the function as an argument

    onclick="UpdateField(fieldname)", but how would I read this into the function to dynamically update the correct text field?
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com


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
  •