Select checkboxes and enable corresponding radio button using jQuery - javascript

I have a checkbox list which contains 6 checkboxes. A corresponding radio button is placed beside each checkbox. When page load, the radio buttons are disabled. When I check the checkbox, I want to enable the radio button beside it. Furthermore, if I uncheck the checkboxes, the radio button beside it will be deselected and disabled again.
The checkbox is for the user to choose items they want, and the radio button is for the user to pick his favourite item.
I have tried this code below but it is not working...
$(document).ready(function () {
$("#rbtnlLeadAgencies input[type='radio']").each(function () {
$("#rbtnlLeadAgencies input[type='radio']").prop('disabled', true);
});
$("#cbxlAgencies input[type='checkbox']").each.change(function () {
if ($("#cbxlAgencies_0 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_0 input[type='radio']").prop('disabled', false);
if ($("#cbxlAgencies_1 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_1 input[type='radio']").prop('disabled', false);
if ($("#cbxlAgencies_2 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_2 input[type='radio']").prop('disabled', false);
if ($("#cbxlAgencies_3 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_3 input[type='radio']").prop('disabled', false);
if ($("#cbxlAgencies_4 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_4 input[type='radio']").prop('disabled', false);
if ($("#cbxlAgencies_5 input[type='checkbox']").is(':checked')) $("#rbtnlLeadAgencies_5 input[type='radio']").prop('disabled', false);
});
});
The markups and jQuery code are on jsfiddle
Any ideas of achieving this behavior?
Thanks in advance!

Try
$(document).ready(function () {
$("#rbtnlLeadAgencies input[type='radio']").prop('disabled', true);
$("#cbxlAgencies input[type='checkbox']").change(function () {
var idx = $(this).closest('tr').index();
var radios = $("#rbtnlLeadAgencies tr").eq(idx).find('input[type="radio"]').prop('disabled', !this.checked);
if(!this.checked){
radios.prop('checked', false);
}
});
});
Demo: Fiddle

DEMO
$(document).ready(function () {
$('input:radio[id^="rbtnlLeadAgencies_"]').prop('disabled', true);
$('input:checkbox[id^="cbxlAgencies"]').change(function () {
var id = this.id.replace('cbxlAgencies_', '');
$("#rbtnlLeadAgencies_" + id).prop('disabled', !this.checked);
});
});
^ attribute-starts-with-selector

As I said in my comment here is the solution which is a bit expensive in markup but more generic :
HTML
<input id="cbxlAgencies_0" data-for-radio="rbtnlLeadAgencies_0" type="checkbox"/>
JS
$(document).ready(function () {
$("input[type='checkbox'][data-for-radio]").change(function () {
$('#' + $(this).data('for-radio')).prop('disabled', !this.checked);
}).change();
});
http://jsfiddle.net/techunter/7M54h/

Try jsfiddle code.
$(document).ready(function () {
$("#rbtnlLeadAgencies input[type='radio']").attr('disabled', true);
$("#cbxlAgencies input[type='checkbox']").change(function () {
$("#rbtnlLeadAgencies_0").prop('disabled', !this.checked);
$("#rbtnlLeadAgencies_1").prop('disabled', !this.checked);
$("#rbtnlLeadAgencies_2").prop('disabled', !this.checked);
$("#rbtnlLeadAgencies_3").prop('disabled', !this.checked);
$("#rbtnlLeadAgencies_4").prop('disabled', !this.checked);
$("#rbtnlLeadAgencies_5").prop('disabled', !this.checked);
});
});

Related

Disable and enable button alternately

I have two buttons but I need to Disable and enable these two buttons alternately. So when I click buttonx it will be disabled and buttony will be enabled, then when I click buttony it will be disabled and buttonx will be enabled again, but in my code it not working.
<script>
$(document).ready(function() {
$("#buttonx").click(function() {
$('button').attr('disabled','disabled');
});
$("#buttony").click(function() {
$('button').removeattr('disabled','disabled');
});
});
A code to start with:
$(document).ready(function() {
$("#buttonx").click(function() {
$('#buttony').removeAttr('disabled');
$(this).attr('disabled', 'disabled');
});
$("#buttony").click(function() {
$('#buttonx').removeAttr('disabled');
$(this).attr('disabled', 'disabled');
});
});
Try this, assuming the buttons in question are the only ones with class button
$(function() {
$(".button").click(function() {
$('button').not(this).attr('disabled',false);
$(this).attr('disabled',true);
});
});
There is also a radio alternative
Making radio buttons look like buttons instead
Try this:
$(document).ready(function() {
$("#buttonx").click(function() {
$(this).attr('disabled','disabled');
$('#buttony').removeAttr('disabled');
});
$("#buttony").click(function() {
$(this).attr('disabled','disabled');
$('#buttonx').removeAttr('disabled');
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<button id="buttonx">
button 1
</button>
<button id="buttony">
button 2
</button>

select all checkbox in header , to select all checkboxes in that column

I have used the code:
<script type="text/javascript">
$(window).load(function () {
$(document).delegate(".checkall", "click", function(event) {
$(this).closest("table").find(':checkbox').attr('checked', this.checked);
});
});
</script>
This code is working fine when I select/deselect the checkbox in header for the first time. But when I again select the checkbox then this code is not working. The checkboxes are not selected.
You should use .prop() instead of .attr(). And you have to check if an element is already checked.
...
var $checkbox = $(this).closest("table").find(':checkbox');
if ($checkbox.is(':checked')) $checkbox.prop('checked', false);
else $checkbox.prop('checked', true);
...

jQuery .on() tr click event using .not() on tr checkbox click?

So my title may be confusing, but it has the right details. I have a table with clickable rows. When clicking the row, the row highlights. The table also has a checkbox column. Clicking the checkbox should NOT highlight or remove highlight from the row. How can I properly use .not() or :not in the .on('click', 'tr', function(){ ... }) ? http://jsfiddle.net/vmu0p2oe/
$('table').on('click', 'tr', function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
//$(this).find(":checkbox").prop("checked", false);
}
else {
$('tr.selected').removeClass('selected');
$(this).addClass('selected');
//$(this).find(":checkbox").prop("checked", true);
}
});
Add this to the beginning of the handler:
if($(e.target).is('input[type=checkbox]')) {
return;
}
This will stop the handler from running if the element clicked is a checkbox.
Fiddle: http://jsfiddle.net/vmu0p2oe/1/
Add a handler for the checkbox that uses stopPropagation() to prevent the click from bubbling out to the tr:
$('table').on('click', ':checkbox', function(e) {
e.stopPropagation();
});
DEMO
How about this:
$('table').on('click', 'tr', function (e) {
var el = e.target;
if ( el.type !== "checkbox"){
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
//$(this).find(":checkbox").prop("checked", false);
} else {
$('tr.selected').removeClass('selected');
$(this).addClass('selected');
//$(this).find(":checkbox").prop("checked", true);
}
}
});
fiddle

Enable CheckBoxList using CheckBox? - ASP.NET

I am having trouble with my code, I was able to implement it using code behind but it was not good as I have sliding panel (using jQuery) which were interfered with on post back.
The CheckBoxList is disabled by default with no auto postback.
I want the CheckBoxList to become enables if the CheckBox is checked.
Currently, I have this code:
$("#BizAppsCheckBox").click(function () {
if (this.checked)
$('#BizAppsCheckBoxList').removeAttr('disabled');
else
$('#BizAppsCheckBoxList').attr('disabled', 'disabled');
});
How can I fix this issue?
Best working solution for me, thanks to the answers:
$(document).ready(function () {
if ($("#BizAppsCheckBox").prop('checked') == false) {
$('#BizAppsCheckBoxList *').prop('disabled', true);
}
else {
$('#BizAppsCheckBoxList *').prop('disabled', false);
}
$("#BizAppsCheckBox").click(function () {
if (this.checked)
$('#BizAppsCheckBoxList *').prop('disabled', false);
else
$('#BizAppsCheckBoxList *').prop('disabled', true) &
$('#BizAppsCheckBoxList *').prop('checked', false);
});
});
Try this :
You should use the ClientID cuz asp.net does provide different ID ( when under container)
$("#<%=BizAppsCheckBox.ClientID%> ").click(function () {
if (this.checked)
$('#<%=BizAppsCheckBoxList.ClientID%>').prop('disabled',false);
else
$('#<%=BizAppsCheckBoxList.ClientID%>').prop('disabled', true);
});
edit
$("#<%=BizAppsCheckBox.ClientID%>").click(function () {
if (this.checked)
$('#<%=BizAppsCheckBoxList.ClientID%> *').prop('disabled', false);
else
$('#<%=BizAppsCheckBoxList.ClientID%> *').prop('disabled', true);
});
Use prop instead of attr, if ClientIDMode is not static then use ClientID of server controls
$("#<%= BizAppsCheckBox.ClientID %>").click(function () {
if (this.checked)
$('#<%= BizAppsCheckBoxList.ClientID %>').prop('disabled', true);
else
$('#<%= BizAppsCheckBoxList.ClientID %>').prop('disabled', false);
});
this will do the trick
$("#<%=BizAppsCheckBox.ClientID%>").click(function () {
$('#<%=BizAppsCheckBoxList.ClientID%>').prop('disabled',!this.checked);
});
or
$("#BizAppsCheckBox").click(function () {
$('#BizAppsCheckBoxList').prop('disabled', !this.checked);
});
try using .on()
$("document").on("click","#<%=BizAppsCheckBox.ClientID%>",function(){
$('#<%=BizAppsCheckBoxList.ClientID%>').prop('disabled',!this.checked);
});

Check all check box does not works the second time in Javascript

The check all check box first time checks and unchecks well but not at the second time. The code is as follows
HTML
<th align="left" width="25" style="color:#FFF; background-color:#3A4048">
<%= check_box_tag "select_all" %>
</th>
JQUERY
$("#select_all").click(function () {
$('td input[type=checkbox]').attr('checked', this.checked);
});
Can any body help me to solve the problem
.prop() is preferred to .attr() for the checked property. Also the .change() event would be more appropriate for a checkbox.
$("#select_all").change(function () {
$('td input[type=checkbox]').prop('checked', $(this).prop('checked'));
});
Try this:
$(function () {
$("#select_all").click(function () {
$('input[type=checkbox]').prop('checked', $(this).prop('checked'));
});
});
Use $(this).prop('checked') Latest jQuery Code.
$("#select_all").click(function () {
$('td input[type=checkbox]').prop('checked', $(this).prop('checked'));
});
$("#select_all").bind('click',function () {
$('td input[type=checkbox]').attr('checked', this.checked);
});

Categories

Resources