Identify the checkbox that is checked, in a group of checkboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helplakshmi
    New Member
    • Dec 2009
    • 14

    Identify the checkbox that is checked, in a group of checkboxes

    I have 2 checkboxes in a form and onclick of these. Once the checkbox is checked, only the fields that are relevant to the checkbox are displayed, with default values in it and to fetch the default values i need to trigger php code. So i can't perform the operation once the whole form is submitted. As the client i am working for does not support AJAX. I can't go for it.

    So i have written onclick = document.formNa me.submit(); Now it is triggering the same page and i am able to write the code. I am not able to differentiate which checkbox is checked.

    I don't want to use the procedure of:- calling javascript and then storing the value of the checkbox in a variable and making this variable as invisible.

    I would like to write something like document.formNa me.submit('chec kbox1'). So that i should be able to handle the value of this or i dont know.

    Please suggest me an alternative method or better approach.
    Last edited by Niheel; Jun 2 '10, 11:28 AM. Reason: cleaned up the question details a little
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you could use individual forms per checkbox … (but maybe I’m misunderstandin g your problem)

    .submit() does not use parameters (this wouldn’t make sense anyway).

    Comment

    • helplakshmi
      New Member
      • Dec 2009
      • 14

      #3
      Originally posted by Dormilich
      you could use individual forms per checkbox … (but maybe I’m misunderstandin g your problem)

      .submit() does not use parameters (this wouldn’t make sense anyway).
      Ok, thanku for the response. But i have many checkbox for which it would be very difficult for me to create many checkboxes. So i want to know which checkbox is checked in a group of them in a single form.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        for simply identifying a single checkbox, assign an unique id to every checkbox.

        Code:
        // returns the ID value of 1 checked checkbox
        function findBox()
        {
            // the general approach, can be simplified depending on HTML
            var i, elem, inp = document.getElementsByTagName("input");
            for (i = inp.length; i--;) {
                elem = inp[i];
                if ("checkbox" === elem.type && elem.id) {
                    return elem.id;
                }
            }
        }

        Comment

        • helplakshmi
          New Member
          • Dec 2009
          • 14

          #5
          Hello ,

          Thanku for ur reply.
          Requirement:- I have a form, in which i have 2 checkboxes. Onclick of a checkbox, values for first set textboxes(that are in the same form) needs to be fetched automatically. onclick of another checkbox the value of select tag and textarea needs to be filled.

          My approach:- I have created 2 checkboxes and onclick of each of the checkboxes i am submittting the same form. From where i can fetch the values of the fields that needs to be filled automatically, using php code.

          print_r($_post) will return Array(checkbox1 =>primary checkbox2=>seco ndary). The value of first and second checkboxes. But i will not able to find which is checked by this click. It just returns values of the checkboxes. But does not give me the info, onclick of which checkbox the form got submitted.

          So, The question is how will i come to know, if the form got submitted, by click of first checkbox or second one.

          Hope i am clear in explaining the questions and waiting for replies.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            My approach:- I have created 2 checkboxes and onclick of each of the checkboxes i am submittting the same form. From where i can fetch the values of the fields that needs to be filled automatically, using php code.
            this sounds like it should be done by AJAX

            Comment

            Working...