delete some checkbox rows while click the delete button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cygsoft
    New Member
    • Jun 2007
    • 29

    delete some checkbox rows while click the delete button

    Hi

    I have one table, in that contain deletebutton and some checkboxes.How many checkbox is selected, that is delete while click the deletebutton .

    Please reply urgently....... ...

    Thank in Advance
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    what have you done so far? post some code so that we may help you with your problem ... try to simplify it to let us see the particular problem you have. as far as i understand: you have a table where you have a checkbox per row ... when you hit a delete-button you want all table-rows to be deleted where the row's checkbox is checked?

    kind regards ...

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      Originally posted by cygsoft
      Please reply urgently....... ...
      First and foremost, your post is no more urgent than anyone else's. Secondly, you haven't asked a question. How can we answer? @_@

      Comment

      • cygsoft
        New Member
        • Jun 2007
        • 29

        #4
        Hi,

        I post my code here ,In this code I have one deletebutton, while i click the deletebutton if checkboxes is not select one alert box is display, if one or more than one checkbox is select confirm box is display that time I want click ok means that selected checkbox rows should delete.


        function checkCheckboxes ()
        {
        var e=document.getE lementsByName(" delId[]");
        for (i=0;i<e.length ;i++)
        {
        if (e[i].checked==true)
        {

        var agree=confirm(' Are you sure you want to delete these email addresses?');

        if (agree)

        { return true; }

        else

        { return false;
        }
        }

        }
        alert('You must select an email address to delete');
        return false;
        }


        Thanks in Advance

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          go through the code an have a look at the comments ... tell me more about your html-code (post some of it with the row you mean and the button) ... ok?

          [CODE=javascript]
          function checkCheckboxes () {
          // gets a collection of nodes that have the name='delId[]';
          var e = document.getEle mentsByName("de lId[]");

          // now you loop through the list of nodes
          for (i = 0; i < e.length; i++) {

          // everytime you find a checked-box you ask for beeing
          // sure to delete
          if (e[i].checked==true) {

          // when agreed the row may be deleted?
          var agree = confirm('Are you sure ...');

          if (agree) {
          // here you need something to delete the row?
          // show me some html of you - you need a reference
          // to the row you want to delete ... when having a
          // look to your html-code we may help you
          return true;
          } else {
          // row shouldn't be deleted
          return false;
          }
          }

          }

          alert('You must select an email address to delete');

          return false;
          }
          [/CODE]

          kind regards ...

          Comment

          • cygsoft
            New Member
            • Jun 2007
            • 29

            #6
            hi,

            I post the HTML code here,


            [HTML]<HTML><HEAD><TI TLE>Delete</TITLE>
            <SCRIPT language="Javas cript">
            function checkCheckboxes ()
            {
            var e=document.getE lementsByName(" delId[]");
            for (i=0;i<e.length ;i++)
            {
            if (e[i].checked==true)
            {

            var agree=confirm(' Are you sure you want to delete these email addresses?');

            if (agree)

            { return true; }

            else

            { return false;
            }
            }

            }
            alert('You must select an email address to delete');
            return false;
            }


            </SCRIPT>

            </HEAD>
            <BODY>
            <FORM name="myform">
            <INPUT onclick="checkC heckboxes()" type="button" value="Delete"> <BR>
            <TABLE id="tblSample" >
            <TBODY>
            <TR id="d1">
            <TD><INPUT id="delId[]" type="checkbox" > a <SPAN style="MARGIN-LEFT: 250px">Edit
            </A>|Delete<BR></SPAN>
            </TD></TR>
            <TR id="d2">
            <TD><INPUT id="delId[]" type="checkbox" > b <SPAN style="MARGIN-LEFT: 250px">Edit
            </A>| Delete<BR></SPAN>
            </TD></TR>
            <TR id="d3">
            <TD><INPUT id="delId[]" type="checkbox" > c <SPAN style="MARGIN-LEFT: 250px">Edit
            </A>| Delete<BR></SPAN>
            </TD></TR>
            <TR id="d4">
            <TD><INPUT id="list1" type="checkbox" > d <SPAN style="MARGIN-LEFT: 250px">Edit
            </A>|Delete<BR></SPAN>
            </TD></TR>
            </TBODY></TABLE></FORM></BODY></HTML>
            [/HTML]Please post using code tags - moderator
            Thanks in Advance
            Last edited by acoder; Jul 2 '07, 10:33 AM. Reason: Code in tags

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              have a look at the following example ... i adapted it note the following things with it:

              - you use getElementsByNa me so set the name not id
              - declare i in the for loop
              - have a look how to remove the line
              - please write all tagnames in lowercase (the same for style-properties) ... i didn't adapt that

              [HTML]<html>
              <TITLE>Delete </TITLE>

              <SCRIPT language="Javas cript">
              function checkCheckboxes () {
              var e = document.getEle mentsByName("de lId");

              for (var i = 0; i < e.length; i++) {
              var c_box = e[i];

              if (c_box.checked == true) {

              var message = 'Are you sure you want to delete?';

              if (window.confirm (message)) {
              var row = c_box.parentNod e;
              var tb = row.parentNode;
              tb.removeChild( row);
              return true;
              } else {
              return false;
              }
              }

              }

              alert('You must select an email address to delete');
              return false;
              }
              </SCRIPT>
              </HEAD>
              <BODY>
              <FORM name="myform">
              <INPUT onclick="checkC heckboxes()" type="button" value="Delete"> <BR>
              <TABLE id="tblSample" >
              <TBODY>
              <TR id="d1">
              <TD><INPUT name="delId" type="checkbox" > a <SPAN style="MARGIN-LEFT: 250px">Edit
              </A>|Delete<BR></SPAN>

              </TD></TR>
              <TR id="d2">
              <TD><INPUT name="delId" type="checkbox" > b <SPAN style="MARGIN-LEFT: 250px">Edit
              </A>| Delete<BR></SPAN>
              </TD></TR>
              <TR id="d3">
              <TD><INPUT name="delId" type="checkbox" > c <SPAN style="MARGIN-LEFT: 250px">Edit
              </A>| Delete<BR></SPAN>

              </TD></TR>
              <TR id="d4">
              <TD><INPUT name="list1" type="checkbox" > d <SPAN style="MARGIN-LEFT: 250px">Edit
              </A>|Delete<BR></SPAN>
              </TD></TR>
              </TBODY>
              </TABLE>
              </FORM>
              </BODY>
              </HTML>
              [/HTML]
              kind regards ...

              Comment

              • cygsoft
                New Member
                • Jun 2007
                • 29

                #8
                hi,

                Thankyou very much for your solution, but it support to delete one row alone, if I select more than one checkbox rows, it does not support, it support to delete first selected checkbox row.
                So please give the solution.

                Thanks in Advance.

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  ;) hey ... i adapted your code to make it work ;)) ... since it didn't work at all ... so have a close look at the code and try to adapt it for yourself first ... when you got questions ... come back and i will be glad to help you out ...

                  hint: if something is to delete e.length > 0 with checked boxes in respect, don't use the confirm within the loop, don't return out of the function during the remove-operation ...

                  kind regards

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    ... hmmm ... had a little time ;)

                    have a look at the following function that may replace the above one:

                    [CODE=javascript]
                    function checkCheckboxes () {
                    var e = document.getEle mentsByName("de lId");
                    var message = 'Are you sure you want to delete?';
                    var row_list = {length: 0};

                    for (var i = 0; i < e.length; i++) {
                    var c_box = e[i];

                    if (c_box.checked == true) {
                    row_list.length ++;

                    row_list[i] = {};
                    row_list[i].row = c_box.parentNod e.parentNode;
                    row_list[i].tb = row_list[i].row.parentNode ;
                    }
                    }

                    if (row_list.lengt h > 0 && window.confirm( message)) {
                    for (i in row_list) {
                    if (i == 'length') {
                    continue;
                    }

                    var r = row_list[i];
                    r.tb.removeChil d(r.row);
                    }
                    } else if (row_list.lengt h == 0) {
                    alert('You must select an email address to delete');
                    }
                    }
                    [/CODE]

                    but the next time ... try something for yourself first!

                    kind regards ...

                    Comment

                    Working...