views:

88

answers:

2

Hi This problem exists only in IE browser, in mozilla its working fine... i have to delete a row on click on checkbox.... Here is my code :

<table width="100%" id="seller">
<tr id="row_1" class="row">
 <td><input type="checkbox" name="delete_seller" id="delete_seller" value="1" /></td>
</tr>
<tr id="row_2" class="row">
 <td><input type="checkbox" name="delete_seller" id="delete_seller" value="2" /></td>
</tr>
</table>
<input type="button" id="delete" name="delete" value="Delete" onclick="delete_row('seller')"/>

    var delete_ids = [];
    function delete_row(tableID){
      var rows = document.getElementsByClassName('row');
      var delItems = document.getElementsByName('delete_seller');

      for(var i=0;i < rows.length;i++){
        if(delItems[i].checked == true){
          delete_ids.push(rows[i].id);
          jQuery(rows[i]).remove();
          i = i-1;
        }
      }
}

Showing error on page : 'checked' is null or not an object.
can one please tell me the fix .

thanks in advance, sri..

+3  A: 

You can replace the loop with jQuery, like this:

delete_ids.push.apply(delete_ids,
    $('tr.row:has(.delete:checked)').remove()
        .map(funcion() { return this.id; })
);

This will select all tr.row element that :has a .delete element that is :checked.
It calls .remove() to remove the rows, then calls .map() to get the rows' IDs, and applies the push function to add them to your array.

You can avoid the push.apply by changing it to

    $('tr.row:has(.delete:checked)').remove()
        .each(funcion() { delete_ids.push(this.id); });

EDIT: Try this:

    $('tr.row:has(.delete_seller:checked)')
        .each(funcion() { delete_ids.push(this.id); })
        .remove();
SLaks
+1 Second time you beat me today. Please do add some more explanatory text though.
MvanGeest
+1, beat me too.
Andy E
My bad , I edited my post , please go through...This is what i'm using. thanks
Srinath
@Srinath: Change `.delete` to match your checkboxes.
SLaks
Hi SLaks,I changed to .delete_seller. but id name is common for all rows. i was displaying the list using loop . should i get all using var delItems = document.getElementsByName('delete_seller'); and use delItems ?
Srinath
@Slaks, Could you please provide me full solution from my code . i was running under issues. this was ragging me since 1 day. thanks .
Srinath
@Srinath: Try my edit. If you have issues, please describe them.
SLaks
var delete_ids = []; function delete_row(tableID){ var rows = document.getElementsByClassName('row'); var delItems = document.getElementsByName('delete_seller'); jQuery('tr.row:has(.delete_seller:checked)').remove() .each(function() { delete_ids.push(this.id); });this is how i am using.please tell me if i'm doing wrong
Srinath
@Srinath: `getElementsByClassName` doesn't exist in IE. Remove `var rows` and `var delItems`.
SLaks
@Slaks , yeah this works great in IE . I added setAttribute("className") and works good. thanks Slaks
Srinath
@Srinath: Then you should accept this answer by clicking the hollow check.
SLaks
A: 

Both your checkboxes and your button are called "delete" and your delItems variable will contain the button as well. The button doesn't have a checked attribute, which I'm guessing is where IE complains about it.

Although in the example you give there are fewer elements with class 'row' than there are elements with name 'delete' which should mean you don't get to the point in the loop where delItems[i] refers to the button. Unless there are more elements class 'row' on the page.

Mario Menger
My bad , I edited my post , please go through...This is what i'm using. thanks
Srinath