Results 1 to 9 of 9

Thread: Please help me with checkbox script

  1. #1
    Join Date
    Apr 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me with checkbox script

    Hi there,

    I am working on a project where I need a checkbox script. I looked on the internet but couldn't find something that I could use.

    I need the following:

    I have a page with 3 checkboxes. For example with red, yellow and white. The moment I check on of them the value must appear somewhere on the page (doesn't matter where) but not in an alertbox or something but really in the page. When I check more than one they have to appear like this: red, yellow, white. Seperated with a commma (The order is not important). Can someone give me an example of a javascript for this?

    Hope you can help me.

    Greets,

    John

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    function valuate(){
    var txt=''
    var colors=document.getElementsByName('color')
    for (var i_tem = 0; i_tem < colors.length; i_tem++)
    if (colors[i_tem].checked)
    txt+=', '+colors[i_tem].value
    txt=txt.replace(/^, /, '')
    document.getElementById('chkr').innerHTML=txt
    }
    
    </script>
    </head>
    <body>
    <input type="checkbox" name="color" value="red" onclick="valuate()"><br>
    <input type="checkbox" name="color" value="yellow" onclick="valuate()"><br>
    <input type="checkbox" name="color" value="white" onclick="valuate()"><br>
    <span id="chkr"></span>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    A bit late but thanks for the script! Just what I need

  4. #4
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    How could I modify the above script to have it post the combined value (red,yellow,white) into a hidden field on a form that would be submitted as one value rather than the individual checkbox values?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This would make your form javascript dependant (not a very good idea on the web) but, to answer your question:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    function valuate(){
    var txt=''
    var colors=document.getElementsByName('color')
    for (var i_tem = 0; i_tem < colors.length; i_tem++)
    if (colors[i_tem].checked)
    txt+=' '+colors[i_tem].value
    txt=txt.replace(/^ /, '')
    document.getElementById('chkr').value=txt
    }
    
    </script>
    </head>
    <body>
    <input type="checkbox" name="color" value="red" onclick="valuate()"><br>
    <input type="checkbox" name="color" value="yellow" onclick="valuate()"><br>
    <input type="checkbox" name="color" value="white" onclick="valuate()"><br>
    <form action="#">
    <input name="colors" type="hidden" id="chkr">
    <input type="submit" value="Go!">
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry if I wasn't clear, but in order to use this with my existing perl forms-handling script, I need to end up with an input tag that looks like this:

    <input name="colors" type="hidden" value="chkr">

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You could do:

    Code:
    <input id="chkr" name="colors" type="hidden" value="chkr">
    If that isn't acceptable, the script and markup could be modified to:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    function valuate(){
    var txt=''
    var colors=document.getElementsByName('color')
    for (var i_tem = 0; i_tem < colors.length; i_tem++)
    if (colors[i_tem].checked)
    txt+=' '+colors[i_tem].value
    txt=txt.replace(/^ /, '')
    document.forms['form1']['colors'].value=txt
    }
    
    </script>
    </head>
    <body>
    <input type="checkbox" name="color" value="red" onclick="valuate()"><br>
    <input type="checkbox" name="color" value="yellow" onclick="valuate()"><br>
    <input type="checkbox" name="color" value="white" onclick="valuate()"><br>
    <form name="form1" action="#">
    <input name="colors" type="hidden" value="chkr">
    <input type="submit" value="Go!">
    </form>
    </body>
    </html>
    However, no matter what you do, the value of the hidden input will be changed by the javascript. That's how forms pass data. If you are already using Perl, there may be a way to use it instead of javascript. That would be better as, it would not leave out non-javascript enabled browsers.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Many thanks for your help, John, got it to work with some tweaks for this particular form.

    I hear you clearly on the less-than-best use of javascript on a form, but it will work for this application.

  9. #9
    Join Date
    Jan 2006
    Location
    At home
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    jscheuer1 You are really senior coder. (bows with respect)

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
  •