Reputation: 7426
I have two radio button on change event i want change button How it is possible? My Code
<input type="radio" name="bedStatus" id="allot" checked="checked" value="allot">Allot
<input type="radio" name="bedStatus" id="transfer" value="transfer">Transfer
Script
<script>
$(document).ready(function () {
$('input:radio[name=bedStatus]:checked').change(function () {
if ($("input[name='bedStatus']:checked").val() == 'allot') {
alert("Allot Thai Gayo Bhai");
}
if ($("input[name='bedStatus']:checked").val() == 'transfer') {
alert("Transfer Thai Gayo");
}
});
});
</script>
Upvotes: 622
Views: 1232329
Reputation: 144659
You can use this
which refers to the current input
element.
$('input[type=radio][name=bedStatus]').change(function() {
if (this.value == 'allot') {
// ...
}
else if (this.value == 'transfer') {
// ...
}
});
Note that you are comparing the value against allot
in both if statements and :radio
selector is deprecated.
In case that you are not using jQuery, you can use the document.querySelectorAll
and HTMLElement.addEventListener
methods:
var radios = document.querySelectorAll('input[type=radio][name="bedStatus"]');
function changeHandler(event) {
if ( this.value === 'allot' ) {
console.log('value', 'allot');
} else if ( this.value === 'transfer' ) {
console.log('value', 'transfer');
}
}
Array.prototype.forEach.call(radios, function(radio) {
radio.addEventListener('change', changeHandler);
});
Upvotes: 1216
Reputation: 2802
Use onchage function
function my_function(val){
alert(val);
}
<input type="radio" name="bedStatus" value="allot" onchange="my_function('allot')" checked="checked">Allot
<input type="radio" name="bedStatus" value="transfer" onchange="my_function('transfer')">Transfer
Upvotes: 15
Reputation: 246
Add the class "pnradio" to the radio buttons, then switch with .attr('id')
<input type="radio" id="sampleradio1" class="pnradio" />
<input type="radio" id="sampleradio2" class="pnradio" />
$('.pnradio').click(function() {
switch ($(this).attr('id')) {
case 'sampleradio1':
alert("xxx");
break;
case 'sampleradio2':
alert("xxx");
break;
}
});
Upvotes: 1
Reputation: 16157
Simple ES6 (javascript only) solution.
document.forms.demo.bedStatus.forEach(radio => {
radio.addEventListener('change', () => {
alert(`${document.forms.demo.bedStatus.value} Thai Gayo`);
})
});
<form name="demo">
<input type="radio" name="bedStatus" value="Allot" checked>Allot
<input type="radio" name="bedStatus" value="Transfer">Transfer
</form>
Upvotes: 14
Reputation: 21
$(document).ready(function () {
$('input:radio[name=bedStatus]:checked').change(function () {
if ($("input:radio[name='bedStatus']:checked").val() == 'allot') {
alert("Allot Thai Gayo Bhai");
}
if ($("input:radio[name='bedStatus']:checked").val() == 'transfer') {
alert("Transfer Thai Gayo");
}
});
});
Upvotes: 2
Reputation: 50767
An adaptation of the above answer...
$('input[type=radio][name=bedStatus]').on('change', function() {
switch ($(this).val()) {
case 'allot':
alert("Allot Thai Gayo Bhai");
break;
case 'transfer':
alert("Transfer Thai Gayo");
break;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="bedStatus" id="allot" checked="checked" value="allot">Allot
<input type="radio" name="bedStatus" id="transfer" value="transfer">Transfer
Upvotes: 175
Reputation: 1037
If the radio buttons are added dynamically, you may wish to use this
$(document).on('change', 'input[type=radio][name=bedStatus]', function (event) {
switch($(this).val()) {
case 'allot' :
alert("Allot Thai Gayo Bhai");
break;
case 'transfer' :
alert("Transfer Thai Gayo");
break;
}
});
Upvotes: 8
Reputation: 789
document.addEventListener('DOMContentLoaded', () => {
const els = document.querySelectorAll('[name="bedStatus"]');
const capitalize = (str) =>
`${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const handler = (e) => alert(
`${capitalize(e.target.value)} Thai Gayo${e.target.value === 'allot' ? ' Bhai' : ''}`
);
els.forEach((el) => {
el.addEventListener('change', handler);
});
});
Upvotes: 6
Reputation: 121
<input type="radio" name="radio" value="upi">upi
<input type="radio" name="radio" value="bankAcc">Bank
<script type="text/javascript">
$(document).ready(function() {
$('input[type=radio][name=radio]').change(function() {
if (this.value == 'upi') {
//write your logic here
}
else if (this.value == 'bankAcc') {
//write your logic here
}
});
</script>
Upvotes: 4
Reputation: 3638
A simpler and cleaner way would be to use a class with @Ohgodwhy's answer
<input ... class="rButton">
<input ... class="rButton">
Script
$( ".rButton" ).change(function() {
switch($(this).val()) {
case 'allot' :
alert("Allot Thai Gayo Bhai");
break;
case 'transfer' :
alert("Transfer Thai Gayo");
break;
}
});
Upvotes: 48