Dynamic checkbox with Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steph

    Dynamic checkbox with Javascript

    Hello,

    Can someone tell me the script to use for having a change on the same page
    when using checkbox function ?

    For example, i would to check one condition and display dynamically a button
    if the condition is checked on the same page.

    Thanks in advance for your help

    Steph


  • Robert

    #2
    Re: Dynamic checkbox with Javascript

    In article <41e1c337$0$294 19$626a14ce@new s.free.fr>,
    "Steph" <mcvalb@hotmail .com> wrote:
    [color=blue]
    > For example, i would to check one condition and display dynamically a button
    > if the condition is checked on the same page.[/color]

    This may help:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <META http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Radio buttons</title>

    <script type="text/javascript">

    function validate(x)
    {
    var checkedButton;

    // Figure out which radio button was pressed

    checkedButton = findValue(x.rec eiveVia);

    var varName = x.theName.value ;
    var varEmail = x.theEmail.valu e;
    var varAddress = x.theAddress.va lue;

    // I changed submitOK to a boolean variable.
    var submitOK = true;

    // Validate email: name and email

    if (checkedButton == "byEmail")
    {

    if (varName == '')
    {
    alert("Please fill in your Name");
    submitOK = false;
    }
    if (varEmail == '')
    {
    alert("Please fill in Email");
    submitOK = false;
    }
    if (varAddress != '')
    {
    alert("Please erase the address field.");
    submitOK = false;
    }

    return submitOK;

    }

    // Validate print: name, email, and address

    else if (checkedButton= ="printed")
    {
    // Error message should be in the order on the form
    if (varName.length == '')
    {
    alert("Please fill in your Name");
    submitOK = false;
    }
    if (varEmail == '')
    {
    alert("Please fill in Email");
    submitOK = false;
    }
    if (varAddress == '')
    {
    alert("You must enter your Address");
    submitOK = false;
    }

    return submitOK;
    }
    else
    {
    alert("You need to select either email or print.");
    return false;
    }

    }

    function vanishHidden(do What)
    {
    // Check if the getElementById method is available
    if (document.getEl ementById)
    {
    document.getEle mentById("hideS pan").style.dis play = doWhat;
    }
    else if (document.all)
    {
    alert("Running an older version of IE.");
    document.all.hi deSpan.style.di splay = doWhat;
    }
    else
    { alert("Cannot change visibility of address field"); }
    }


    function hideHidden(doWh at)
    {
    // Check if the getElementById method is available
    if (document.getEl ementById)
    {
    document.getEle mentById("vanis hSpan").style.v isibility = doWhat;
    }
    else if (document.all)
    {
    alert("Running an older version of IE.");
    document.all.va nishSpan.style. visibility = doWhat;
    }
    else
    { alert("Cannot change display value of address field"); }
    }

    // See which radio button is selected and return its value
    function findValue(obj)
    {
    var i, theValue;
    theValue = null;

    for (i=0;i<obj.leng th;i++)
    {
    if (obj[i].checked == true)
    {
    theValue = obj[i].value;
    break;
    }
    }

    return theValue;
    }
    </script>

    </head>

    <body>

    <p>Please try out our form.</P>
    <P>This form uses the CSS display
    and visibility style attributes. When you click on the
    radio button email, Javascript code uses the display attribute
    property of hidden to exclude the address field from the display.
    No space will be taken up in the window.
    When you click on the no radio button, Javascript code uses the
    visibility attribute property of none to make the literature
    catagories invisible. Space will be taken up in the window.</p>

    <form name="myForm"
    action="http://www.notavalidur l.com"
    method="GET"
    onsubmit="retur n validate(docume nt.forms['myForm']);">
    <p>
    <input type="radio"
    name="receiveVi a"
    value="printed"
    onclick="vanish Hidden('');">
    Printed brochure</p>
    <p>
    <input type="radio"
    name="receiveVi a"
    value="byEmail"
    onclick="vanish Hidden('none');
    document.forms['myForm'].theAddress.val ue = '';">
    Via Email</p>
    <p>Name:<br>
    <input type="text"
    name="theName"
    size="20"><br>< br>
    Email:<br>
    <input type="text" name="theEmail" size="20">
    <br><br>

    <span id="hideSpan">
    Postal address:<br>
    <input type="text" name="theAddres s" size="40">
    </span>
    </p>
    <p>
    Do you wish to receive additional literature?
    <br>
    <input type="radio"
    name="literatur e"
    value="yes"
    onclick="hideHi dden('visible') ";>&nbsp;Yes&nb sp;&nbsp;
    <!-- use visibility. -->
    <span id="vanishSpan" >
    <input type="checkbox"
    name="gardening "
    valuegardening> &nbsp;Garden ing
    <input type="checkbox"
    name="kitchen"
    valuekitchen>&n bsp;Kitchen
    <input type="checkbox"
    name="vacation"
    valuevacation>& nbsp;Vacation
    <!-- Just get it done. I know there are better ways. -->
    <br>&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;

    &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;
    <input type="checkbox"
    name="office"
    valueoffice>&nb sp;Office
    <input type="checkbox"
    name="secondhom e"
    valuesecondhome >&nbsp;Second Home
    </span>

    <br>
    <input type="radio"
    name="literatur e"
    value="no"
    onclick="hideHi dden('hidden');
    var d=document.form s['myForm'];
    d.elements['gardening'].checked=false;
    d.elements['kitchen'].checked=false;
    d.elements['vacation'].checked=false;
    d.elements['office'].checked=false;
    d.elements['secondhome'].checked=false; ">
    No
    </p>
    <p><input type="submit"
    border="0"
    value="Submit form"
    width="75"
    height="17"
    ALT="Submit button"></p>

    </form>

    <script type="text/javascript">
    // In the case of a reload, the radio button may already be clicked.
    // This code needs to be after the form.
    var x = document.forms["myForm"];
    if (x.receiveVia[0].checked == true)
    { vanishHidden('' );}
    else if (x.receiveVia[1].checked == true)
    { vanishHidden('n one');}
    else
    { ;}

    if (x.literature[0].checked == true)
    { hideHidden('vis ible');}
    else if (x.literature[1].checked == true)
    { hideHidden('hid den');}
    else
    { ;}

    </script>
    </body>
    </html>


    Robert

    Comment

    • RobG

      #3
      Re: Dynamic checkbox with Javascript

      Steph wrote:
      [...][color=blue]
      > For example, i would to check one condition and display dynamically a button
      > if the condition is checked on the same page.[/color]

      The script below uses an onload function to fire the click when the
      document loads. This is for two reasons:

      1. If the user has javascript disabled or their browser does not
      support it, the text box will still display and they can use it.
      Otherwise, it will not be shown when the checkbox is checked.

      2. You can set the checkbox to checked or not and the text input will
      display/not display on opening the page without you having to change
      the properties of the checkbox.

      One note of caution: if the user puts something into the input then
      unchecks the checkbox, the inupt is hidden but the text in it will
      still be submitted. So maybe you want to disable it when it is not
      displayed.

      You can also use:

      inp.style.visib ility = 'visible' or 'hidden'

      if you want the text box to still take up space on the page when
      hidden.

      Lastly, if the page is big, the onload will fire after all content is
      loaded, so your text box may appear the disappear. To prevent that,
      move the onload to a script immediately after the input so the body tag
      becomes:

      <body>

      and put:

      <script type="text/javascript">
      clickIt('aForm' ,'aCheckbox');
      </script>

      immediately after the text input. I think putting it in the body
      onload is cleaner, but obviously having the input flash on then off may
      be disconcerting for users.



      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      <html><head>
      <title>Dynami c text box</title>
      <script type="text/javascript">
      function showHide(chk,in p){
      if (chk.checked) {
      inp.style.displ ay = '';
      } else {
      inp.style.displ ay = 'none';
      }
      }

      function clickIt(frm,chk ) {
      var x = document.forms[frm].elements[chk];
      if (x) x.click();
      }
      </script>
      </head>
      <body onload="clickIt ('aForm','aChec kbox');">
      <form action="" name="aForm">
      <label for="aCheckbox" >Click me&nbsp;<input type="checkbox"
      name="aCheckbox " id="aCheckbox" onclick="
      showHide(this,t his.form.txtInp ut);
      ">
      </label><br>
      <input type="text" size="30" name="txtInput" >
      </form>
      </body></html>


      --
      Rob

      Comment

      • Steph

        #4
        Re: Dynamic checkbox with Javascript

        "RobG" <rgqld@iinet.ne t.auau> a écrit dans le message de news:
        aslEd.74$Ud4.79 58@news.optus.n et.au...[color=blue]
        > Steph wrote:
        > [...][color=green]
        >> For example, i would to check one condition and display dynamically a
        >> button if the condition is checked on the same page.[/color]
        >
        > The script below uses an onload function to fire the click when the
        > document loads. This is for two reasons:
        >
        > 1. If the user has javascript disabled or their browser does not
        > support it, the text box will still display and they can use it.
        > Otherwise, it will not be shown when the checkbox is checked.
        >
        > 2. You can set the checkbox to checked or not and the text input will
        > display/not display on opening the page without you having to change
        > the properties of the checkbox.
        >
        > One note of caution: if the user puts something into the input then
        > unchecks the checkbox, the inupt is hidden but the text in it will
        > still be submitted. So maybe you want to disable it when it is not
        > displayed.
        >
        > You can also use:
        >
        > inp.style.visib ility = 'visible' or 'hidden'
        >
        > if you want the text box to still take up space on the page when
        > hidden.
        >
        > Lastly, if the page is big, the onload will fire after all content is
        > loaded, so your text box may appear the disappear. To prevent that,
        > move the onload to a script immediately after the input so the body tag
        > becomes:
        >
        > <body>
        >
        > and put:
        >
        > <script type="text/javascript">
        > clickIt('aForm' ,'aCheckbox');
        > </script>
        >
        > immediately after the text input. I think putting it in the body
        > onload is cleaner, but obviously having the input flash on then off may
        > be disconcerting for users.
        >
        >
        >
        > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
        > <html><head>
        > <title>Dynami c text box</title>
        > <script type="text/javascript">
        > function showHide(chk,in p){
        > if (chk.checked) {
        > inp.style.displ ay = '';
        > } else {
        > inp.style.displ ay = 'none';
        > }
        > }
        >
        > function clickIt(frm,chk ) {
        > var x = document.forms[frm].elements[chk];
        > if (x) x.click();
        > }
        > </script>
        > </head>
        > <body onload="clickIt ('aForm','aChec kbox');">
        > <form action="" name="aForm">
        > <label for="aCheckbox" >Click me&nbsp;<input type="checkbox"
        > name="aCheckbox " id="aCheckbox" onclick="
        > showHide(this,t his.form.txtInp ut);
        > ">
        > </label><br>
        > <input type="text" size="30" name="txtInput" >
        > </form>
        > </body></html>
        >
        >
        > --
        > Rob[/color]

        Thanks Rob,
        I will test and let you know ...
        Steph


        Comment

        • Steph

          #5
          Re: Dynamic checkbox with Javascript

          "Robert" <rccharles@my-deja.com> a écrit dans le message de news:
          rccharles-6E0DEF.21102009 012005@individu al.net...[color=blue]
          > In article <41e1c337$0$294 19$626a14ce@new s.free.fr>,
          > "Steph" <mcvalb@hotmail .com> wrote:
          >[color=green]
          >> For example, i would to check one condition and display dynamically a
          >> button
          >> if the condition is checked on the same page.[/color]
          >
          > This may help:
          >
          > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          > "http://www.w3.org/TR/html4/loose.dtd">
          > <html>
          > <head>
          > <META http-equiv="content-type" content="text/html; charset=iso-8859-1">
          > <title>Radio buttons</title>
          >
          > <script type="text/javascript">
          >
          > function validate(x)
          > {
          > var checkedButton;
          >
          > // Figure out which radio button was pressed
          >
          > checkedButton = findValue(x.rec eiveVia);
          >
          > var varName = x.theName.value ;
          > var varEmail = x.theEmail.valu e;
          > var varAddress = x.theAddress.va lue;
          >
          > // I changed submitOK to a boolean variable.
          > var submitOK = true;
          >
          > // Validate email: name and email
          >
          > if (checkedButton == "byEmail")
          > {
          >
          > if (varName == '')
          > {
          > alert("Please fill in your Name");
          > submitOK = false;
          > }
          > if (varEmail == '')
          > {
          > alert("Please fill in Email");
          > submitOK = false;
          > }
          > if (varAddress != '')
          > {
          > alert("Please erase the address field.");
          > submitOK = false;
          > }
          >
          > return submitOK;
          >
          > }
          >
          > // Validate print: name, email, and address
          >
          > else if (checkedButton= ="printed")
          > {
          > // Error message should be in the order on the form
          > if (varName.length == '')
          > {
          > alert("Please fill in your Name");
          > submitOK = false;
          > }
          > if (varEmail == '')
          > {
          > alert("Please fill in Email");
          > submitOK = false;
          > }
          > if (varAddress == '')
          > {
          > alert("You must enter your Address");
          > submitOK = false;
          > }
          >
          > return submitOK;
          > }
          > else
          > {
          > alert("You need to select either email or print.");
          > return false;
          > }
          >
          > }
          >
          > function vanishHidden(do What)
          > {
          > // Check if the getElementById method is available
          > if (document.getEl ementById)
          > {
          > document.getEle mentById("hideS pan").style.dis play = doWhat;
          > }
          > else if (document.all)
          > {
          > alert("Running an older version of IE.");
          > document.all.hi deSpan.style.di splay = doWhat;
          > }
          > else
          > { alert("Cannot change visibility of address field"); }
          > }
          >
          >
          > function hideHidden(doWh at)
          > {
          > // Check if the getElementById method is available
          > if (document.getEl ementById)
          > {
          > document.getEle mentById("vanis hSpan").style.v isibility = doWhat;
          > }
          > else if (document.all)
          > {
          > alert("Running an older version of IE.");
          > document.all.va nishSpan.style. visibility = doWhat;
          > }
          > else
          > { alert("Cannot change display value of address field"); }
          > }
          >
          > // See which radio button is selected and return its value
          > function findValue(obj)
          > {
          > var i, theValue;
          > theValue = null;
          >
          > for (i=0;i<obj.leng th;i++)
          > {
          > if (obj[i].checked == true)
          > {
          > theValue = obj[i].value;
          > break;
          > }
          > }
          >
          > return theValue;
          > }
          > </script>
          >
          > </head>
          >
          > <body>
          >
          > <p>Please try out our form.</P>
          > <P>This form uses the CSS display
          > and visibility style attributes. When you click on the
          > radio button email, Javascript code uses the display attribute
          > property of hidden to exclude the address field from the display.
          > No space will be taken up in the window.
          > When you click on the no radio button, Javascript code uses the
          > visibility attribute property of none to make the literature
          > catagories invisible. Space will be taken up in the window.</p>
          >
          > <form name="myForm"
          > action="http://www.notavalidur l.com"
          > method="GET"
          > onsubmit="retur n validate(docume nt.forms['myForm']);">
          > <p>
          > <input type="radio"
          > name="receiveVi a"
          > value="printed"
          > onclick="vanish Hidden('');">
          > Printed brochure</p>
          > <p>
          > <input type="radio"
          > name="receiveVi a"
          > value="byEmail"
          > onclick="vanish Hidden('none');
          > document.forms['myForm'].theAddress.val ue = '';">
          > Via Email</p>
          > <p>Name:<br>
          > <input type="text"
          > name="theName"
          > size="20"><br>< br>
          > Email:<br>
          > <input type="text" name="theEmail" size="20">
          > <br><br>
          >
          > <span id="hideSpan">
          > Postal address:<br>
          > <input type="text" name="theAddres s" size="40">
          > </span>
          > </p>
          > <p>
          > Do you wish to receive additional literature?
          > <br>
          > <input type="radio"
          > name="literatur e"
          > value="yes"
          > onclick="hideHi dden('visible') ";>&nbsp;Yes&nb sp;&nbsp;
          > <!-- use visibility. -->
          > <span id="vanishSpan" >
          > <input type="checkbox"
          > name="gardening "
          > valuegardening> &nbsp;Garden ing
          > <input type="checkbox"
          > name="kitchen"
          > valuekitchen>&n bsp;Kitchen
          > <input type="checkbox"
          > name="vacation"
          > valuevacation>& nbsp;Vacation
          > <!-- Just get it done. I know there are better ways. -->
          > <br>&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;
          >
          > &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;
          > <input type="checkbox"
          > name="office"
          > valueoffice>&nb sp;Office
          > <input type="checkbox"
          > name="secondhom e"
          > valuesecondhome >&nbsp;Second Home
          > </span>
          >
          > <br>
          > <input type="radio"
          > name="literatur e"
          > value="no"
          > onclick="hideHi dden('hidden');
          > var d=document.form s['myForm'];
          > d.elements['gardening'].checked=false;
          > d.elements['kitchen'].checked=false;
          > d.elements['vacation'].checked=false;
          > d.elements['office'].checked=false;
          > d.elements['secondhome'].checked=false; ">
          > No
          > </p>
          > <p><input type="submit"
          > border="0"
          > value="Submit form"
          > width="75"
          > height="17"
          > ALT="Submit button"></p>
          >
          > </form>
          >
          > <script type="text/javascript">
          > // In the case of a reload, the radio button may already be clicked.
          > // This code needs to be after the form.
          > var x = document.forms["myForm"];
          > if (x.receiveVia[0].checked == true)
          > { vanishHidden('' );}
          > else if (x.receiveVia[1].checked == true)
          > { vanishHidden('n one');}
          > else
          > { ;}
          >
          > if (x.literature[0].checked == true)
          > { hideHidden('vis ible');}
          > else if (x.literature[1].checked == true)
          > { hideHidden('hid den');}
          > else
          > { ;}
          >
          > </script>
          > </body>
          > </html>
          >
          >
          > Robert[/color]

          Thanks again for your help !
          Steph


          Comment

          Working...