jQuerry Get Radio Button Value

About Radio button

A radio button is a graphical user interface element that allows the user to choose one option from a set of options. Radio buttons are typically presented as small circles that can be selected by clicking on them. Once a radio button is selected, it will be filled with a dot or a small circle, indicating that it has been chosen. When a radio button is selected, any other radio buttons in the same group will automatically be deselected.

Radio buttons are typically used when there is a limited number of options and only one option can be selected at a time. For example, a survey might use radio buttons to allow the user to select their favorite color from a list of options, or a form might use radio buttons to allow the user to indicate their gender.

In HTML, radio buttons are created using the ‘<input>’ element with the ‘type’ attribute set to “radio”. The ‘name’ attribute is used to group radio buttons together, so that only one button from a group can be selected at a time.

Create Radio button in jQuerry:

In jQuery or JavaScript, you can create a radio button by creating a new <input> element with the type attribute set to “radio” and then appending it to an existing HTML element.

JavaScript
var radioButton = $('<input>', {
    type: 'radio',
    name: 'groupName',
    value: 'optionValue',
});

This creates a new <input> element with the type attribute set to “radio”, the name attribute set to “groupName” and the value attribute set to “optionValue”.

You can append the new radio button to a specific element on the page by using the appendTo() method:

JavaScript
radioButton.appendTo('#container');

In this example, the new radio button will be appended to an element with the ID “container”. You can also use .append() method to add the radio button to an element.

Example:

JavaScript
$('#container').append(radioButton);

Create Radio Button in JavaScript:

You can also create radio buttons in JavaScript by creating a new <input> element and setting its attributes.

JavaScript
var radioButton = document.createElement("input");
radioButton.type = "radio";
radioButton.name = "groupName";
radioButton.value = "optionValue";

You can then append the radio button to an existing element on the page:

JavaScript
document.getElementById("container").appendChild(radioButton);

In this example, the new radio button will be appended to an element with the ID “container”.

How to get the value from radio button with jQuery

To determine which radio button is selected with jQuery, you can use the ‘:checked’ selector to select the selected radio button, and then use the ‘val()’ method to retrieve its value.

Here is an example:

JavaScript
var selectedValue = $('input[name=groupName]:checked').val();

In this example, ‘groupName’ is the name attribute shared by all the radio buttons in a group, and ‘$(‘input[name=groupName]:checked’)’ selects the selected radio button from that group. The ‘val()’ method is then used to retrieve the value of the selected radio button.
You can also use ‘.filter(‘:checked’)’ to select the checked radio button and then get the value using ‘.val()’

JavaScript
var selectedValue = $('input[name=groupName]').filter(':checked').val();

You can also use ‘.attr(‘value’)’ instead of ‘.val()’ to get the value of the radio button.

JavaScript
var selectedValue = $('input[name=groupName]:checked').attr('value');

All of the above examples assume that radio buttons have the same name attribute, and that only one radio button can be selected at a time.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.