Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use radio on change event?

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> 
like image 548
Ashish Mehta Avatar asked Oct 31 '12 07:10

Ashish Mehta


People also ask

What is the event for radio button?

A radio button fires the change event after you select it. How it works: First, register an event handler to the change event of the body . When a radio button is clicked, its change event is bubbled to the body.

How do you checked radio button is changed in jQuery?

Now, to detect when that radio box is changed, you can easily call the jQuery 'change' event inside your jQuery document ready function: $(document). ready(function(){ $('#my_radio_box'). change(function(){ alert('Radio Box has been changed!

How to handle the change event of a radio button?

Note that if you want to handle every change of the value, you use the input event instead. A radio button fires the change event after you select it. The following example shows how to handle the change event of the radio buttons: First, register an event handler to the change event of the body.

How to handle every change of the value of a radio?

Note that if you want to handle every change of the value, you use the input event instead. A radio button fires the change event after you select it. The following example shows how to handle the change event of the radio buttons:

How to show the radio button selected message based on radio button?

When a radio button is clicked, its change event is bubbled to the body. This technique is called event delegation. Then, show a corresponding message based on which radio button is selected. Using JavaScript change event for checkboxes

What happens when a radio button is clicked?

When a radio button is clicked, its change event is bubbled to the body. This technique is called event delegation. Then, show a corresponding message based on which radio button is selected. Similar to radio buttons, checkboxes fire the change event after selection, whether checked or unchecked.


1 Answers

You can use this which refers to the current input element.

$('input[type=radio][name=bedStatus]').change(function() {     if (this.value == 'allot') {         alert("Allot Thai Gayo Bhai");     }     else if (this.value == 'transfer') {         alert("Transfer Thai Gayo");     } }); 

http://jsfiddle.net/4gZAT/

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); }); 
like image 177
undefined Avatar answered Sep 28 '22 03:09

undefined