1

I have a Radio Buttons for Gender and Drop down for Courses,

I have Dynamic values in Jquery.

How can I get the selected options in both Gender and Courses.

Here is my Fiddle Demo

I am providing my Code here too:

HTML:

Male <input type="radio" name="gender" id="male" value="1"> - 

Female <input type="radio" name="gender" id="female" value="2"> 

<br>

<select name="courses" id="courses">
    <option value="0">Choose Courses</option>
    <option value="1">PHP</option>
    <option value="2">Jquery</option>
    <option value="3">HTML</option>
</select>

JS:

$(function() {
    //these are the values I got, these values are dynamic
    var gender = 2;
    var course = 3; 

}

4 Answers 4

2

GET

$("input[type='radio']").val();// get radio box value
$("#courses").val();// get dropdown with id "courses value

SET

$("input[type='radio'][value=2]").attr("checked","checked");// set radio box value with checked propety
$("#courses").val(3);// set dropdown with id "courses value

you can also get value when you change value by

$("input[type='radio'],#courses").change(function(){
    alert($(this).val());
});

SEE UPDATED DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

No no i dont want values, i want to make it selected using Jquery.
Radio is not selected
Can you do the same with radio on Id bases ? cz i have multiple radios
@Code Hunter Id must be unique for html elements, use the name property or add a class to the radio buttons and select them by using that class.
Can you do it with name or class ?
1

Try this one:

Html:

Male <input type="radio" name="gender" id="male" value="1" class="gender"/>         
Female <input type="radio" name="gender" id="female" value="2" class="gender"/> 

Script: SET:

 var gender = 2;
 var course = 3;     
 $(".gender").val(gender).prop('checked', true);
 //$(".gender").val(gender).attr('checked', true);
 $("#courses").val(course);

GET:

 $(".gender:checked").val();
 $("#courses").val();

Change event:

$(".gender,#courses").change(function(){
    alert($(this).val());
});

Demo:
Select radio using class.

Select radio by name.

With change event.

Using attr instead of prop.

2 Comments

Prop is a new function. And i m using lod library of Jquwey, what is the alternate of prop ? Can u do it with out Prop
attr is the alternate for prop. I've updated my answer check the demo.
0
var gender = 2;
var course = 3;

$('input:radio').eq(gender-1).prop('checked', true);

$('select').val(course);

Comments

0
var gender = 2;
var course = 3;

$('input:radio[name="gender"]').filter('[value="' + gender + '"]').attr('checked', true);
$("#courses").val(course);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.