Dynamically create checkbox based on the output in the responseText from server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarega
    New Member
    • Oct 2008
    • 82

    Dynamically create checkbox based on the output in the responseText from server

    Hi,
    Here I have an array returned that is returned in responseText from the server. Based on the output that is returned by the server I have to dynamically create a checkbox.
    Code:
    function StateChanged() {
    if(xmlHttp.readyState == 4) {
    xyz=xmlHttp.responseText;
    if(xyz!="false") {
    alert("true");
    Here I have to add a function to create a checkbox 
    }
    }
    }
    Somebody please help me??
    I was trying to use document.create Element() but it did not work.
    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you probably didn't append the created element to the document tree?

    Comment

    • sarega
      New Member
      • Oct 2008
      • 82

      #3
      now am able to create a checkbox if a button is clicked. I wanted to check whether that particular checkbox if clicked another javascript function has to be evoked, am not able to figure out how to do that...

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        something like
        Code:
        document.getElementById(checkbox_id).addEventListener("click", callback_function, false);
        // "change" instead of "click" should work too
        // does not work in IE, workarounds available

        Comment

        • sarega
          New Member
          • Oct 2008
          • 82

          #5
          Is there a possibility to add a text next to the dynamically created checkbox

          Comment

          • sarega
            New Member
            • Oct 2008
            • 82

            #6
            And also onclick of the checkbox can we find is the checkbox been checked or unchecked

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by sarega
              Is there a possibility to add a text next to the dynamically created checkbox
              yes
              Originally posted by sarega
              And also onclick of the checkbox can we find is the checkbox been checked or unchecked
              yes

              (for the sake of 20 chars)

              Comment

              • sarega
                New Member
                • Oct 2008
                • 82

                #8
                Can we pass arguments to the callback function in the eventlistener

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  not directly, you have three options:
                  1. use a closure. the parameter must be known at closure creation time
                    Code:
                    function parent_function(elem, param)
                    {
                        var temp = function() { callback_with_param(param); }
                        elem.addEventListener("click", temp, false);
                    }
                  2. use a property of the used element
                    Code:
                    function callback()
                    {
                        var my_var = this.param;
                        // do cool stuff here using my_var
                    }
                    
                    function myFunc(elem, para);
                    {
                        elem.param = para;
                        elem.addEventListener("click", callback, false);
                    }
                  3. use an anonymous function. this event can not be removed!
                    Code:
                    elem.addEventListener("click", function() { yourFunc(param); }, false);

                  Comment

                  • sarega
                    New Member
                    • Oct 2008
                    • 82

                    #10
                    I have dynamically added many checkboxes to the form and each of the checkboxes have different name,id and value. When the form is submitted how can I retrieve only those checkboxes which have been checked? The number of checkboxes in the form will vary everytime.
                    I will have something like this if i try to print all the posted values in the form
                    [red] => on [blue] => on
                    I want the red and blue, for my further processing

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      as far as I know, only the selected checkboxes will show up in the submitted data.

                      you can also filter off empty (i.e. all values that evaluate as false) values by
                      Code:
                      // PHP
                      $array = array_filter($_POST);

                      Comment

                      • sarega
                        New Member
                        • Oct 2008
                        • 82

                        #12
                        am trying to use both attachEvent and addEventListene r but attchEvent is not working in IE
                        Code:
                        if(document.getElementById('chk').addEventListener) {
                                        document.getElementById('chk').addEventListener("click",function() { abc(colors); },false);
                                } else if(document.getElementById('chk').attachEvent) {
                                        alert("here");
                                        document.getElementById('chk').attachEvent('onClick', function() { abc(colors); });
                                } else {
                                        alert("Your browser does not support any handlers");
                                }
                        Can somebody tell me what is the problem?

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          try (Javascript is case sensitive)
                          Code:
                          document.getElementById('chk').attachEvent('[B]onclick[/B]', function() { abc(colors); });
                          otherwise you can use various cross browser functions ( e.g. addEvent() ) available via google
                          Last edited by Dormilich; Jan 30 '09, 08:54 AM. Reason: mentioning addEvent()

                          Comment

                          • sarega
                            New Member
                            • Oct 2008
                            • 82

                            #14
                            I have tried onclick also but even that did not work

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              maybe you should then use one of the addEvent() functions, they're working.

                              possibly attachEvent() doesn't like the anonymous function...?

                              Comment

                              Working...