title: Using variables when referencing DOM jQuery DOM objects
body: <p>I'm trying to understand why you can't construct a jQuery DOM object with a string and variable (even if the variable is also a string).</p>
  
  <pre><code>//Works
  var test1 = $('#test');
  test1.addClass('success');
  
  //Works
  var test2 = '#test';
  $(test2).addClass('success');
  
  //Fails
  var test3 = 'test';
  $('.' + test3).addClass('success');
  
  //Fails
  var id = 'test';
  var test4 = '.' + id;
  $(test4).addClass('success');
  </code></pre>
  
  <p>Here's <a href="http://jsfiddle.net/QeWLu/" rel="nofollow">a jsFiddle</a> to demonstrate.</p>
  
  <p>I feel like I'm probably missing something basic, but I can't find anything in the jQuery documentation about this.</p>
  
  <p><strong>EDIT:</strong> Doh, I should have noticed I used the wrong selectors in the example. I selected mcpDESIGNS' answer since it did indeed fix my jsFiddle. However, I was still getting an error in my code. ("Uncaught Exception: Syntax error, unrecognized expression: .") Here's what was going on in case anyone's interested:</p>
  
  <p>I was trying to take the current hash in the URL, save the string as a variable, use that to construct a jQuery DOM object, and pass that object into a function.</p>
  
  <pre><code>var hashTag = window.location.hash;
  hashTag = hashTag.substr(1);
  
  currentObject = $('.'+hashTag);
  setSpriteX(currentObject);
  </code></pre>
  
  <p>I figured out that I wasn't accounting for when there wasn't a hash in the URL. All I needed to do was add <code>hashTag = 'defaulthash';</code> before I set <code>currentObject</code>.</p>