I have a panel named pBody and I can clear its controls by using these codes.
clear all textboxes, uncheck radio buttons and checkboxes:
$('#pBody input').val('');
clear only textboxes:
$("#pBody").find("input[type=text]").val('');
or
$("#pBody").find("input:text").val('');
But after clicking clear button I want make some radio buttons checked, for example first radio buttons in the group.
Thanks in advance.
You can use input[type=radio]:first selector to select first radio-button in the list.
Or if you want that every gruop first radio button get selected you can use
$('element').each(function(){
$('input[type=radio]:first', this).attr('checked', true);
});
For check the first radio button as default you can use the below code
$('#pBody').find('input:radio').first().prop({checked:true});
or
$('#pBody').find('input:radio').first().attr({checked:true});
and for checkboxes use
$('#pBody').find('input:checkbox').first().prop({checked:true});
or
$('#pBody').find('input:checkbox').first().attr({checked:true});
Related
How to Enable a button in html if the radio button is checked. this is to be done dynamically as if the radio button gets unchecked the button should also get disabled automatically. is there any possible way to do this using javascript or Jquery..code should should be in php
You can't uncheck a radio button, but if you want to do it on a checkbox, here is a suggestion using jquery:
$("#checkBoxID").on("change", function(e) {
$("#buttonID").prop("disabled",!$("#checkBoxID").is(":checked"));
});
This will set the disabled attribute on the button to the opposite of the checked attribute.
•Box Checked? Disabled Attribute on button set to false
•Box unchecked? Disabled attribute on button set to true
Updates dynamically as the checkbox is selected.
-- edit --
Here is a link to a JSFiddle of this working as described and requested.
I'm using a Twitter Bootstrap 3 form with (4) Radio form controls. When a user clicks on any of the four radio controls, an <input type="text"> appears. How do I make it, so that the first input, on any radio option select is "autofocus".
To see code, if you may, please visit: https://von.host/cart.php?a=add&pid=20
It just seems silly to have the user click a radio button, than click again on the input field and type, than click once more on submit button...instead of three clicks, I wish to make it two.
p.s. - i added "autofocus" to all (4) input fields, but it only worked for the first one...of course ^ _ ^
I think, you can this only with a little part of javascript :) for example:
$('input[type="radio"]').focus(function(){
$(this).parent().find('input[type="text"]').focus();
});
In nutshell, this code pass the focus for the input field, when the radio give it.
Keep the autofocus attribute in the first one and use this jQuery code:
$('.domainoptions .option input:radio[name="domainoption"]').change(
function(){
$(this).parent().next().find('input[type="text"]').first().focus();
}
);
I use this very nice plugin in my project called jQuery Dropdown Check List (https://code.google.com/p/dropdown-check-list/) specifically one of example which is named: 'Single select with radio buttons instead of checkboxes'.
One big problem with this example is that I am not able to (I was trying to do this by jQuery) set radio button unchecked. I was using for example:
$("input:radio").attr("checked", false);
Or
$("input:radio").removeAttr("checked");
And unfortunately nothing. Can anyone give some advice how fix this thing?
Try this:
- For check:
$("input:checkbox").attr("checked", "checked");
or
$("input:checkbox").prop('checked', true);
-For uncheck:
$("input:checkbox").removeAttr("checked");
or
$("input:checkbox").prop('checked', false);
I tried both methods in the demo page(http://dropdown-check-list.googlecode.com/svn/trunk/doc/dropdownchecklist.html) for the "Single select with radio buttons instead of checkboxes", and turns out to be fine: the selected radio button has removed. However, the value stays in the span because remove selected radio cannot remove selected value.
My suggestion is that after you uncheck the radio, also set a html space to the span, it removes the selected value and also keep the height of its container.
This plugin uses checkbox not radio inputs so you must use $('input:checkbox') or $('input[type="checkbox"]') as your selector!
Click here to see a demo!
I'm wondering how I can use JavaScript to accomplish the following:
I have an input area with five dropdownlists, and each one has a corresponding radio button. When a dropdownlist is clicked to make a selection, I would like the radio button paired with it to become checked right then. Any help would be appreciated.
Its kind of simple really. Here is a small code for you.
ID the 5 listboxes as "sel1", "sel2" ,...
ID the 5 radio buttons "rad1", rad2,.. Make sure the radio buttons have same name though!
Next, put this function in as a javascrip:
function makeSelection(radioid){
document.getElementById(radioid).checked=true;
}
In the select boxes, attach onchanged listeners:
ie;
<select ... onchange="makeSelection('rad1');" >.. for the 1st select box,
<select ... onchange="makeSelection('rad2');" >.. for the 2ns select box, etc.
I want to have a group of radiobuttons that are inactive until a checkbox is clicked.
In other words, when the user checks a checkbox, it will allow the user to use the radiobuttons. If the user unchecks a checkbox, it will make the radiobuttons inactive, so the user cannot select them anymore.
Is there a solution using jQuery that I can use to achieve this?
Assuming that the radiobuttons have all the same name, then you can use the attribute selector for this:
$('#checkboxid').click(function() {
$('input[name="radiobuttonname"]').attr('disabled', !this.checked);
});
This will enable the radiobuttons if the checkbox is checked and disable when unchecked.