title: Use variable as selector in function
body: <p>On click, I want to get the name of the closest div and then look for all div's, that have this name attribute and add a class to them.</p>
  
  <p>Here is my code:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div class="wrapper"&gt;
    &lt;div class="container" name="button1"&gt;
      &lt;div class="button"&gt;this is p #1&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div name="button1"&gt;
    somewhere else
  &lt;/div&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>$('.wrapper').on("click", '.button', function() {
     var attrname = $(this).closest('.container').attr('name');
     $("div[name=attrname]").each(function() {
       $(this).addClass("classtobeadded");
     });
  });
  </code></pre>
  
  <p>But it is not working. So, how can I use the variable in here:</p>
  
  <pre><code>$("div[name=attrname]").each(function()
  </code></pre>
  
  <p>Here is the <a href="http://jsfiddle.net/4268vznu/1/" rel="nofollow noreferrer">fiddle</a>: </p>