2

Please help me with this issues.

i'm going to get the index number of checked checkbox array which reside in a html table, but each time i alert the each of checkbox's index inside .each() , its will return 0 for each loop.

but this code will work if i remove the table and all its tr and td (leave the two checbox alone)

my jquery code:

function notify() {

               $("input[type = 'checkbox']:checked").each(function(){
               alert($(this).index());  /*always return zero when the check[] inside a table or div */                
               });           
           }

function main() {


        $("input[type = 'button']").click(notify);
}


$(document).ready(main);

my html code:

 <table>
   <tr><td>
    <input type='checkbox' name='check[]' />
   </td><td>
   <input type='checkbox' name='check[]' />
   </td> </tr>  
 </table>   
<input type='button' name='button' value='ok'/>

this is the link to my code in jsfiddle

2
  • 1
    .index() without arguments returns the position of the node among its own siblings. And in every <td> your checkboxes are the first (and the only) ones - so you're getting 0 See api.jquery.com/index Commented Apr 19, 2013 at 3:45
  • Good reason why this problem arise..i will study to find another solution. Commented Apr 19, 2013 at 14:03

2 Answers 2

4

Try this

$("input:checkbox:checked").each(function(index,value){
          alert($(this).parents('td').index());  
  }); 

http://jsfiddle.net/cuNE6/9/

Sign up to request clarification or add additional context in comments.

2 Comments

still not working..the problem arise when the checbox[] elements in different td..if i remove the td, it works.
its working only when two the checkbox is ticked..but if only one checkbox is ticked, their always show 0 even though the 2nd checkbox is ticked.
0

This is probably what you are looking for. SInce you are wrapping your check boxes in td's, why not find the index of td

[Demo]

function notify() {

    $("input[type = 'checkbox']:checked").each(function () {
        alert($(this).parents('td').index());
    });
}

1 Comment

Thanks a lot..finally..thanks alot..for my case i need to add the td parrent. alert($(this).parents('td').parents('tr').index());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.