title: jQuery: how to do something only if element exist?
body: <p>I'm new to jQuery and i think this question was answered many times. But i can't suggest how to find it. While writing jQuery code, i often write constructions where selector is repeated:</p>
  
  <pre><code>if( $( ".someclass .someclass #someid" ).filter( ":visible" ) )
  {
    $( ".someclass .someclass #someid" ).filter( ":visible" ).click();
    // This must be executed only if element was found.
    return;
  }
  // This must be executed only if first element not found
  if( $( ".someotherclass #someotherid" ).filter( ":hidden" ) )
  {
    $( ".someotherclass #someotherid" ).filter( ":hidden" ).show();
    return;
  }
  </code></pre>
  
  <p>As you can see, selector text is repeated. Is it any way to re-use result of last matched selector inside "if()"? For example, something like:</p>
  
  <pre><code>if( $( ".someclass .someclass #someid" ).filter( ":visible" ) )
  {
    $( ":last-result" ).click();
    return;
  }
  if( $( ".someotherclass #someotherid" ).filter( ":hidden" ) )
  {
    $( ":last-result" ).show();
    return;
  }
  </code></pre>