Reputation: 39
Actually, I have many checkboxes and textfields coming from database through single code. i want when i checked perticular checkbox, then a textfied in front of checkbox is bieng editable. so how can i do that ??
Here is my PHP code
<div class="my">
<?php
while($ass = mysql_fetch_array($rs))
{ ?>
<input type="checkbox" name="fees_name[]" id="fee_name" value="<?php echo $ass['fees_name'];?>"> <?php echo $ass['fees_name']; ?>
<input id="fee_amt" class="fee_amt" type="text" placeholder="amt" name="fees_amt[]" >
<br><br>
<?php } ?> // my while loop end here
</div>
and my Javascript
<script>
$(document).ready(function(){
$('#fee_name').click(function() {
$('#fee_amt').prop('disabled', !$(this).is(':checked'));
});
});
</script>
but it worked on only first checkbox. Anyone can tell my what can i do for others and how can i create different ID's for Each textfield ?? my textfields created by only one code. .
Upvotes: 0
Views: 718
Reputation: 2917
Here is the working code what you need,
$( "input[type=checkbox]" ).on( "click", function(){
if($(this).is(':checked')) {
$(this).next().prop('disabled', false);
} else {
$(this).next().prop('disabled', true);
}
});
HTML
<input type="checkbox" name="fees_name[]">
<input type="text" disabled="disabled" /><br/>
<input type="checkbox" name="fees_name[]">
<input type="text" disabled="disabled" /><br/>
<input type="checkbox" name="fees_name[]" >
<input type="text" disabled="disabled" /><br />
<input type="checkbox" name="fees_name[]">
<input type="text" disabled="disabled" /><br />
<input type="checkbox" name="fees_name[]">
<input type="text" disabled="disabled" />
If you need to give name then you could use input[name='fees_name[]']
as,
$( "input[name='fees_name[]']" ).on( "click", function(){
if($(this).is(':checked')) {
$(this).next().prop('disabled', false);
} else {
$(this).next().prop('disabled', true);
}
});
Upvotes: 1
Reputation: 74738
You can do this way:
so workaround is:
try this below:
note: You have to disable the input type text first.
<input class="fee_amt" type="text" placeholder="amt"
name="fees_amt[]" disabled/>
then apply the script
$('.fee_name').change(function() {
$(this).siblings('.fee_amt').prop('disabled', !this.checked);
});
Upvotes: 0
Reputation: 5461
the following line is rendered multiple times:
<input type="checkbox" name="fees_name[]" id="fee_name" value="<?php echo $ass['fees_name'];?>"> <?php echo $ass['fees_name']; ?>
it means that the id fee_name
won't be unique, and this is bad practice (and the reason that it works only for the first item).
try something like this:
instead of id="fee_name"
put class="fee_name"
(there's no problem with repeating class names, unlike ID's).
and replace your jQuery code with the following:
$(function(){
$('.fee_name').click(function(){
$(this).next('.free_amt').prop('disabled', $(this).is(':checked'));
});
});
hope that helped.
Upvotes: 0