ItSolutionStuff.com

How to Set Radio Button Checked Based on Value using JQuery?

By Hardik Savani • July 21, 2023
jQuery

Hi All,

This post is focused on jquery set radio button checked based on value. In this article, we will implement a jquery set radio button checked by id. we will help you to give example of set radio button checked jquery by value.

This article will give you simple example of set radio button checked jquery by name. You just need to some step to done set radio button checked jquery by id.

Let's see bellow solution and example:

Solution:

$('.btn1').click(function(){

var value = 1;

$("input[name=type][value=" + value + "]").prop('checked', true);

});

$('.btn2').click(function(){

var value = 2;

$("input[name=type][value=" + value + "]").prop('checked', true);

});

Solution:

<!DOCTYPE html>

<html>

<head>

<title>jquery set radio button checked based on value - itsolutionstuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>

<body>

<h1>jquery set radio button checked based on value - itsolutionstuff.com</h1>

<label>

<input type="radio" name="type" value="1"> User

</label>

<label>

<input type="radio" name="type" value="2"> Admin

</label>

<button class="btn1">Select User</button>

<button class="btn2">Select Admin</button>

<script>

$(document).ready(function(){

$('.btn1').click(function(){

var value = 1;

$("input[name=type][value=" + value + "]").prop('checked', true);

});

$('.btn2').click(function(){

var value = 2;

$("input[name=type][value=" + value + "]").prop('checked', true);

});

});

</script>

</body>

</html>

I hope it can help you...

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check If Checkbox is Checked or Not in JQuery?

Read Now →

How to Split String to Array By Comma using JQuery?

Read Now →

How to Remove Empty or Null Values from JSON using JQuery?

Read Now →

JQuery Chosen Multi Select Dropdown Example

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to Allow Only One Checkbox Checked at a Time in JQuery?

Read Now →

How to Convert Line Breaks to br in jQuery ?

Read Now →