0

I need to select a radio input with name and value in jquery

In this example how you select element with name SiblingSex and value female

<form action="">
<input type="radio"  name="SiblingSex" value="male">Male<br>
<input type="radio"  name="SiblingSex" value="female">Female


<input type="radio"  name="ParentSex" value="male">Male<br>
<input type="radio"  name="ParentSex" value="female">Female

</form>

i need some thing like

$('input[name="SiblingSex"]' /*GENDER SELECTOR */)
10
  • 1
    Doing such a thing doesn't make any sense! you're getting value with val() Why do you need it as a selector? Commented Mar 18, 2014 at 10:46
  • @DhavalMarthak it seems to me OP just want to have female value of SiblingSex not from female from ParentSex. Commented Mar 18, 2014 at 10:47
  • 1
    @Jai If it is then only the name selector is enough! isn't it? Commented Mar 18, 2014 at 10:49
  • but that would get you the male value too. Commented Mar 18, 2014 at 10:49
  • @DhavalMarthak Ofcourse i dont want to get the val , it just a example, i have been edited the question Commented Mar 18, 2014 at 10:49

2 Answers 2

1

You can add another attribute selector with this:

$('input[name="SiblingSex"][value="female"]').val();

the above line would give you values every time whether it is checked or not.

so if you only want to have the value when it is checked too then add :checked

$('input[name="SiblingSex"][value="female"]:checked').val();
Sign up to request clarification or add additional context in comments.

2 Comments

Nice , Its working , i was trying some thing like $('input[name="SiblingSex",value="female"]').val() , i missed []
@vimalraj.s you should also add :checked too.
1

Just have a look on the Demo on my JS Fiddle Code

Shows the two scenario:

1) when you want the value without selecting radio button.


2) when you want value after selecting radio button.

or may be the thing that you want is here::

JS Fiddle Demo

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.