I know it can be done with input boxes using keyup paste on JQuery but what about Radio buttons?
Example:
User Selects option from Area A (radio button) and it selects(syncs) area B
<!--AREA A (Source) -->
<input id="radio_A" name="radio_A" type="radio" value="Value1" />
<input id="radio_A2" name="radio_A" type="radio" value="Value2" />
<!--AREA B (Target)-->
<input id="radio_B" name="radio_B" type="radio" value="Value1" />
<input id="radio_B2" name="radio_B" type="radio" value="Value2" />
$('input[name=radio_A]').change(function() {
var index = $(this).index('input[name=radio_A]');
$('input[name=radio_B]:eq(' + index + ')').attr('checked','checked');
});
Code: http://jsfiddle.net/BUbHf/1/
Try this:
$("#radio_A").click(function() {
$("#radio_B").attr("checked", $(this).attr("checked"));
});
$("#radio_A2").click(function() {
$("#radio_B2").attr("checked", $(this).attr("checked"));
});
Example fiddle
$("input[name='radioGroup1'][value='"+$("input[name='radioGroup2']:checked").val()+"']").prop("checked",true);
Related
I have been attempting to allow radio buttons to be deselected using jQuery, but I am running into issues with the prop function. When this code runs, my conditional ($(e.currentTarget).prop('checked')) always evaluates to true.
Here is a fiddle which demonstrates my issue: Jsfiddle
FYI: I am using jQuery 1.8.2, and I cannot update it because it is a legacy project with many dependencies. Also, I MUST use radio buttons per the client's request.
Javascript:
$("input[name=optionMedia]").click(function(e) {
if ($(e.currentTarget).prop('checked')) {
$(e.currentTarget).prop('checked', false);
}
});
Html:
<input class="bigSizeInput" type="radio" id="audioVideo" name="optionMedia" value="1"/>
<input class="bigSizeInput" type="radio" id="showReel" name="optionMedia" value="2" />
You can do it like this
$('input.bigSizeInput').mouseup(function() {
if ($(this).is(':checked')) {
setTimeout(function() {
$('input.bigSizeInput:checked').prop('checked', false);
}, 1)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="bigSizeInput" type="radio" id="audioVideo" name="optionMedia" value="1" />
<input class="bigSizeInput" type="radio" id="showReel" name="optionMedia" value="2" />
This is because checked is a property. If its there it is true, if its not it is not true. Hence you either should switch to a checkbox or use
$("..").removeProp('checked')
Using checkbox to check/uncheck is better than radio button. But if you want to use radio button, you need to check if radio is checked, copy it using and remove checked attribute of copied element and then insert it after target radio. At the end remove original radio.
$(document).on("mousedown", "input[name=optionMedia]", function(e) {
if (this.checked)
$(this).clone().prop('checked', false).insertAfter(this).end().remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<input class="bigSizeInput" type="radio" id="audioVideo" name="optionMedia" value="1"/>
<input class="bigSizeInput" type="radio" id="showReel" name="optionMedia" value="2" />
I have a div with 3 checkboxes inside div. I want to get all of them to string with jQuery.
Im using this theme http://almsaeedstudio.com/preview/
the checkbox in forms --> general elements.
its changes the HTML of checkbook for the design,
div:
<div id="some_id">
<input type="checkbox" value="1">
<input type="checkbox" value="2">
<input type="checkbox" value="3">
</div>
Jquery:
$("#some_div input[type='checkbox']").on('ifChecked', function(event){
arr='';
$('div[aria-checked="true"]').find("input").each(function(){
arr=arr+","+$(this).val();
});
console.log(arr);
});
The script works, but I have 2 problems:
It doesn't get the value of check-box that just been checked. For example:
if I check 1, arr will be empty.
if I check 1 and 2, arr will be ",1".
if I check 1 2 3, arr will be ",1,2".
How could I run this script when "unchecked"?
I tried:
$("#some_div input[type='checkbox']").on('click', function(event){
It doesn't work on bootstarp checkboxes. This is only a issue in bootstrap, in regular html there is no problem to get values with "click".
thanks.
First: Change - -> =
<input type="checkbox" value="1">
Second: Change name inside the input field
<input type="checkbox" value="1" name="checkgroup">
<input type="checkbox" value="2" name="checkgroup">
<input type="checkbox" value-"3" name="checkgroup">
Third: you can give it some ID for easy access:
<input type="checkbox" id="checkbox1" value="1" name="checkgroup">
Forth: Use jquery to do with it as you will:
$("#checkbox1").click('click', function(){.. /*do something*/ .. });
when dealing with checkboxes, it might be better to use the onChange. Here is a demo
try something like that:
<div id="some_id">
<input type="checkbox" value="1" name="checkgroup" />
<input type="checkbox" value="2" name="checkgroup" />
<input type="checkbox" value="3" name="checkgroup" />
</div>
JS:
$(document).ready(function () {
$('input[type="checkbox"]', '#some_id').on('change', function () {
var arr = '';
$('input[type="checkbox"]:checked', '#some_id').each(function (i, ele) {
if (arr.length > 0) {
arr += ', ';
}
arr += $(ele).val();
});
console.log(arr);
});
});
I want to create the conditions in the radio button.
so I'm making a survey app with jquery ajax. every question has the option (radio button) and the button next to to the next question. without changing link
I want to create the conditions in the radio button. If select YES then it will go to the next question. If you select NO then the next question will pass 2
can help me please?
have you tried doing an attribute on your radio button like arnum that will track the n^th number of radio button.
<div arnum="3">
<input type="radio" skipval="3" value="yes">
<input type="radio" skipval="1" value="no">
</div>
<div arnum="4">
<input type="radio" value="something">
<input type="radio" value="else">
</div>
then:
$('div[arnum] input[type=radio][skipval]').on('click',function(){
var skipval = parseInt($(this).attr('skipval'));
var arnum = parseInt($(this).parent().attr('arnum'));
arnum++;
for(;arnum<arnum+skipval;arnum++){
$('div[arnum='+arnum+']').attr('disable','disable');
}
$('div[arnum='+arnum+']').focus();
});
this code on click skips to the next question using skipval and disables everything in between.
y this:
<input type="radio" name="test" value="1" />yes<br />
<input type="radio" name="test" value="2" />no
<div id="question2" style="display:none">
<br />Question 2<br />
</div>
<script>
$('input[name=test]').change(function () {
if ($(this).val() == 1) {
$('#question2').show();
} else {
$('#question2').hide();
}
})
</script>
Below is the my html code:
<input type="checkbox" id="multiOptions" />IsForMultioptions
<input type="radio" value="1" name="option">option1
<input type="radio" value="2" name="option">option2
If I select checkbox i.e. multiOptions then all radio buttons should be convert into checkboxes.
and If I uncheck the checkbox i.e. multiOptions then all checkboxes should convert in radio buttons.
thanks in advance.
You'll need to actually remove and recreate the elements, because IE doesn't let you change the type of an input after it's created.
jQuery makes this fairly easy with replaceWith:
$("selector for the input to change").replaceWith('<input type="checkbox">');
So for instance:
$('input[type="radio"][name="option"]').each(function() {
$(this).replaceWith('<input type="checkbox" name="option" value="' + this.value + '">');
});
Or if the values may contain characters requiring entities:
$('input[type="radio"][name="option"]').each(function() {
var rep = $('<input type="checkbox" name="option">');
rep.attr("value", this.value);
$(this).replaceWith(rep);
});
Instead of replacing the elements you can have two groups of which one is hidden depending on the checkbox:
HTML
<input type="checkbox" id="multiOptions">IsForMultioptions</input>
<div id="radios">
<input type="radio" value="1" name="option">option1</input>
<input type="radio" value="2" name="option">option2</input>
</div>
<div id="checkboxes">
<input type="checkbox" value="1" name="option">option1</input>
<input type="checkbox" value="2" name="option">option2</input>
</div>
jQuery
$(document).ready(function() {
$('#checkboxes').hide();
$('#multiOptions').on('click', function() {
if ($(this).is(':checked')) {
$('#radios').hide();
$('#checkboxes').show();
}
else {
$('#radios').show();
$('#checkboxes').hide();
}
});
});
jsFiddle example.
<input type="radio" name="animal" id="cat" value="Cat" />
<label for="cat">Cat</label>
<input type="radio" name="animal" id="radio-choice-2" value="choice-2"/>
<label for="radio-choice-2">Dog</label>
<input type="radio" name="animal" id="radio-choice-3" value="choice-3" />
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="animal" id="radio-choice-4" value="choice-4" />
<label for="radio-choice-4">Lizard</label>
<input type="button" value="Get Radio Button Value" onclick="getValue();"
I want to get the value of selected Radio Button on click of button I had written the code
function getValue () {
alert("Value is :"+$('input[name=animal]:checked').val());
}
But its not working and getting Undefined
Update:
I have used js of Jquery and jquerymobile
for jquery its for fine but as i am using it for mobile app so i need both the js library so in second case it didnot work.
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
[Solved]
Hi I think you are getting the value without checking any radio button.
Load your page then select any one of radio button and then call getValue function
Alternatively you can put any radio button selected by default.
e.g.
replace first radio button with
<input type="radio" name="animal" id="cat" value="Cat" checked="true"/>
<label for="cat">Cat</label>
Try this
$("#myform input[type='radio']:checked").val();
OR
var myRadio = $('input[name=myRadio]');
//Use the filter() function, not find(). (find() is for locating child elements, whereas //filter() searches top-level elements in your selection.)
var checkedValue = myRadio.filter(':checked').val();