Search code examples
jquerybuttoncss-selectorsradio-button

Get only unique names from more elements


I have dynamically rendered HTML that lists an undetermined number of radio buttons whose names represent some ids in a database.

I need to collect all of the unique names of the radios.

Here is an example:

<input name="721" type="radio" value="A" />        
<input name="721" type="radio" value="I" />     

<input name="722" type="radio" value="A" />        
<input name="722" type="radio" value="I" />     

<input name="723" type="radio" value="A" />        
<input name="723" type="radio" value="I" />     

Checking the radios is NOT required, so using a selected or checked attribute won't work.

Is there an elegant way to get all the unique names of the radios using jQuery? I know I can just loop through each of the radios, but I'm hoping that jQuery has a more elegant way to do this?


Solution

  • jsBin demo

    var arr = [];
    
    $.each( $('input:radio'), function(){
    
      var myname= this.name;
      if( $.inArray( myname, arr ) < 0 ){
         arr.push(myname); 
      }
    
    });
    
    alert(arr); // 721,722,723