Good day.

There is a code:

<div class="optionCatergory sade"> <label class='searchClick'><input type='radio' checked='checked' value='A'>A</label> <label class='searchClick' ><input type='radio' value='b'>B</label> </div> <div class="optionCatergory view"> <label class='searchClick' ><input type='radio' checked='checked value='1'>1</label> <label class='searchClick' ><input type='radio' value='2'>2</label> </div> 

How to add to the array, say, new Arr все type='radio' checked='checked when the page loads?

  • Need an array of values ​​or the nodes themselves? - user31688

2 answers 2

You can use the function each () to iterate over the selected elements, the following code is obtained:

 var array = new Array(); $(".optionCatergory input[type='radio']:checked").each(function () { array.push($(this).val()); }); 

Example with output to console

  • There is a small bug, here is the corrected version: jsfiddle.net/Lwpsmnrk/1 - user31688
  • @TheDoctor, the error seems to be in html, which was copied from the question :) - MasterAlex
  • @MasterAlex, yup :) - user31688
  • honestly decided differently, thanks - Sergalas

I did not quite understand what you want to add to the array, but here is a selector for inputs. Execution occurs on page load. Judging by the tag, you have nothing against jquery)) Then do what you want with them

 $(function () { var allInputs = $(".optionCatergory input[type='radio']:checked"); //do something }); 
  • I asked for an array, and to shorten the code, you could write var allInputs = $ (". optionCatergory input: radio: checked"); or just var allInputs = $ (". optionCatergory input: checked"); I thought of it before myself - Sergalas
  • But I need to array. - Sergalas
  • @Sergalas like $ (...). Map (function (o) {return $ (o) .val ()}) - nörbörnën
  • no, nothing happened - Sergalas
  • @Sergalas you hardly tried hard var a = $ .map ($ (". OptionCatergory input: radio: checked"), function (o) {return $ (o) .val ()}); - nörbörnën