Autolycus Autolycus - 6 months ago 12
Javascript Question

jquery checkbox attribute value

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

Answer

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"></script>

<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>