www.webdeveloper.com
Results 1 to 5 of 5

Thread: Help please?

  1. #1
    Join Date
    Mar 2003
    Posts
    12

    Help please?

    I have 17 checkboxes- I need to check which ones have been checked (max 3 - I have a function that checks the max amount already) the thing I need help with is assigning the three checked boxes to three variables say 1, 2 and 3. I want this to be within its own function - I will make the variables global so I can access them from other functions (my insert function).

    My checkboxes are set up as follow:

    <td><input type='checkbox' name='reason1' value='1' onClick="countChoices(this)">Internal rattling</td>

    Can anyone help please

  2. #2
    Join Date
    Jan 2003
    Location
    Cardiff, Wales, UK
    Posts
    208
    I think your best bet would be for all of the checkboxes to have the same name, e.g. "response".

    Then, you could loop though to find the selected checkboxes using code such as:

    Code:
    var index = 0;
    var answers = new Array();
    for (var i=0; i < response.length; i++) {
        if (response[i].checked) {
            answers[index] = response.value;
            index++;
        }
    }
    Obviously, the value of each of your text boxes will have to equal the literal string you want each one to represent.

    Hope that helps...

  3. #3
    Join Date
    Jan 2003
    Location
    Peg City
    Posts
    638
    Well, here is the function from the check boxes thread that I wrote for you.

    function countChoices(field) {
    count = 0;
    max = 3; // max. number allowed at a time

    boxes = new Array();
    for(i = 0; i < document.frmHandsetRepair.reasons.length; i++){
    boxes[i] = document.frmHandsetRepair.reasons[i].checked;
    count += (boxes[i] ? 1 : 0);
    }

    if (count > max) {
    alert("You can only choose up to " + max + " choices! \nUncheck an option if you want to pick another.");
    field.checked = false;
    }
    }

    If you want the values, do the following:

    function getValues(){

    var index = 0;
    var max=3;
    var values = new Array();

    for (var i=0; i < document.frmHandsetRepair.reasons.length; i++) {
    if (document.frmHandsetRepair.reasons[i].checked) {
    values[index] = document.frmHandsetRepair.reasons[i].value;
    index++;
    }
    }

    and call this function within the countChoices function here:

    if(count > max) {
    getValues();
    }

    after this, your values array should contain the values you want.

    Havik

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    First of all you should not have started a new thread for the problem which is being discussed already in th eforum.

    http://forums.webdeveloper.com/showt...&threadid=6738

    Here try this link..and see if this is ehat you want to get done.

    http://68.145.35.86/temp/rcamp-CheckBoxes.html

    Cheers

    Khalid

  5. #5
    Join Date
    Jan 2003
    Location
    Peg City
    Posts
    638
    exactly, that's what I was mentioning.

    Havik

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles