JQuery selection of form switchers

I have a form and I use this document.forms["form-0"].answerN[0]; to select a specific switch, however, I was not able to do this using jQuery.

I tried $('forms["form-0"]') and $('forms[0]') to go to the form, but this did not work, and both work with a long path.

+7
source share
5 answers

you can access this (jQuery v1.6)

 $('form > input:radio').prop('id'); 

Demo

Update

How can you use the form with the name or id attribute just like this

  $('form[name="firstForm"] > input:radio').prop('id'); 

Demo

+10
source

Try it like this: $("form input:radio")

For more help, follow this link.

+2
source

form-0 is the name of the form. You should write something like

  $ ('form [name = form-0]') to select the form.

to get the switch, you have to move on to use the children

So

  $ ('form [name = form-0]'). children ('radio [name = answerN]: first-child')

hope this should work

+1
source

If you have this form:

 <form action="" method="post" id="myForm"> <input type="radio" name="myRadioButton" value="0" class="radio" id="firstRadioButton" /> Value </form> 

You can select elements by their identifier:

selects a radio button

 $("#firstRadioButton) 

selects a form

 $("#myForm") 

Or select all the switches:

 $("#myForm input.radio").each(function(){ alert($(this).val()); }); 

And without using identifiers:

HTML:

 <form action="" method="post"> <input type="radio" value="1" /> Value 1 <br /> <input type="radio" value="2" /> Value 2 <br /> </form> 

JQuery

 alert($("form").first().find("input:radio").first().val()); 

Example: Look here

+1
source

You can try this to view all the forms on your page:

  $ ('forms'). each (function (id) {
     console.log (id, $ (this));
 });
0
source

All Articles