title: Dynamically get radio button group name using jQuery
body: <p>I would like to ask if there is a way to get a radio button group name dynamically i.e. optimize the following 2 click functions into one by having <code>[name=some_variable]</code>. </p>
  
  <p>I tried:</p>
  
  <pre class="lang-js prettyprint-override"><code>$('input:radio').click(function() {
      alert($('input:radio:checked').attr('name'));
  });
  </code></pre>
  
  <p>but it always returns me the name of the first radio button group clicked.</p>
  
  <pre class="lang-js prettyprint-override"><code>$(document).ready(function(){
      $('input:radio[name=q1]').click(function() {
          var ans1 = $('input[name=q1]:radio:checked').val();
          getUserAnswer(1, ans1);
      });
  
      $('input:radio[name=q2]').click(function() {
          var ans2 = $('input[name=q2]:radio:checked').val();                 
          getUserAnswer(2, ans2);
      });                             
  });
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;body&gt;
      &lt;ol&gt;
          &lt;li&gt;&lt;p&gt;Q1&lt;br /&gt;
              &lt;input type="radio" id="q1A" name="q1" value="A" /&gt;Q1 A&lt;br /&gt;
              &lt;input type="radio" id="q1B" name="q1" value="B" /&gt;Q1 B&lt;br /&gt;
              &lt;input type="radio" id="q1C" name="q1" value="C" /&gt;Q1 C&lt;br /&gt;
              &lt;input type="radio" id="q1D" name="q1" value="D" /&gt;Q1 D&lt;br /&gt;
          &lt;/p&gt;&lt;/li&gt;
          &lt;li&gt;&lt;p&gt;Q2&lt;br /&gt;
              &lt;input type="radio" id="q2A" name="q2" value="A" /&gt;Q2 A&lt;br /&gt;
              &lt;input type="radio" id="q2B" name="q2" value="B" /&gt;Q2 B&lt;br /&gt;
              &lt;input type="radio" id="q2C" name="q2" value="C" /&gt;Q2 C&lt;br /&gt;
              &lt;input type="radio" id="q2D" name="q2" value="D" /&gt;Q2 D&lt;br /&gt;           
          &lt;/p&gt;&lt;/li&gt;
      &lt;/ol&gt;
  &lt;/body&gt;
  </code></pre>