views:

66

answers:

1

I have this span that is in a loop and produce multiple results.
what I want is the attribute recordID value when its checked

<span  id="row" index="<?php print $i; ?>" recordID="<?php print $row->ID; ?>"  Color="<?php print $row->Color; ?>">  
<input type="checkbox" style="left:10;" />  
</span>

I am using this in my js file to check if the checkbox was clicked

var test =  $('input:checkbox:checked').val();      
alert(test);

And all I get is on
how can I get attribute value
Thanks

+2  A: 

This will get you the attribute value

$('#row').attr('recordID');

If you wanted to get the value when the checkbox gets checked, here is an example

js file

$(document).ready(function(){
 $('input:checkbox').change(function(){
  if($(this).attr('checked')){
   alert($(this).parent().attr('recordID'));
  }
 });
});

To see how many rows have been checked:

JavaScript

$(document).ready(function(){
 $('#check').click(function(event){
  event.preventDefault();
  alert($('input:checkbox:checked').size());
 });
});

HTML

<span  id="row1" recordID="1">  
 <input type="checkbox" style="left:10;" />  
 <input type="checkbox" style="left:10;" />  
 <input type="checkbox" style="left:10;" />  
 <input type="checkbox" style="left:10;" />  
</span>
<span  id="row2" recordID="2">  
 <input type="checkbox" style="left:10;" />  
</span>
<span  id="row3" recordID="3">  
 <input type="checkbox" style="left:10;" />  
</span>
<span  id="row4" recordID="4">  
 <input type="checkbox" style="left:10;" />  
</span>
<button id='check'>Check Num Rows</button>

To check within an individual span

$(document).ready(function(){
    $('#check').click(function(event){
        event.preventDefault();
        alert($('#row1 input:checkbox:checked').size());
    });
});

Here is the complete sample code i am using to make the example you need

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt;

<script type='text/javascript'>
$(document).ready(function(){
    $('input:checkbox').change(function(){
        alert($(this).parent().find(':checkbox:checked').size());
    });
});
</script>
</head>
<body>
<span  id="row1" recordID="1">  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
</span><br/><br/>
<span  id="row2" recordID="2">  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
</span><br/><br/>
<span  id="row3" recordID="3">  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
</span><br/><br/>
<span  id="row4" recordID="4">  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
    <input type="checkbox" style="left:10;" />  
</span>
</body>
</html>
wowo_999
Ho w do I check how many rows were clicked. Thanks
I can check how many check boxes were checked using alert($("input[type=checkbox]:checked").length); but I want to be able to only check within the Span id=row. how would I do that Thanks
wowo_999 Thank you for your help. But for some reason it doesnt give me the total number of boxes checked within a span thanks
In the code listed above, i tied it to clicking <button id="check">Check</button>. If you dont want to tie it to anything, you can remove that, but alert($('#row1 input:checkbox:checked').size());will print the value of the row with id="row1"
wowo_999
The alert give me 0. I tried it several times but no luck. Thanks!
I used all these below with no luck alert($("#row input[type=checkbox]:checked").length); alert('Row1 id: '+$('#row1 input:checkbox:checked').size()); alert('Proc Class '+$('#proc input:checkbox:checked').size());
Can you post a link to the page you are working on?
wowo_999
sorry its on my local machine and cant post the link. Is there anything I could be missing. Can I get you an answer using developers tool? Thanks
I just added a complete example to my answer, paste it into an html file and it should do what you need it to.
wowo_999
I see what you did with example. I think its the $(document).ready(function() that doesnt work when I put it in my code. I am using frames and also unlike the example, my js file is an external js file.
aah for some reason this worked alert($(this).parents().find('#row input:checkbox:checked').size()); Wonder why