title: Jquery check if element exist and if conditon
body: <p>I have code like this:</p>
  
  <pre><code>&lt;?php if ($this-&gt;session-&gt;userdata('permission') == 4 || $this-&gt;session-&gt;userdata('permission') == 3) : ?&gt;
              &lt;select name="caffe" id="caffe"&gt;
                  &lt;?php foreach($caffe as $key) : ?&gt;
                  &lt;option value="&lt;?php echo $key['id_caffe'] ?&gt;"&gt;&lt;?php echo $key['name'] ?&gt;&lt;/option&gt;
                  &lt;?php endforeach; ?&gt;
              &lt;/select&gt;
              &lt;div id="gll_select"&gt;
                  &lt;script&gt;
                   var caffe = $("#caffe").val();
                   var cff_name = $("#caffe option:first").text();
                   $("#gll_select").load("&lt;?php echo base_url() ?&gt;form/galleries", {id : caffe}, function(){                 
                            $("#gll").on('change', function(){
              var cff_name  = $("#caffe option:selected").text();
              get_gallery(cff_name);
              console.log(cff_name);
          }); 
                   });  
                  &lt;/script&gt;
              &lt;/div&gt;
  
              &lt;?php else : ?&gt;
  
              &lt;input type="hidden" name="caffe_name" id="caffe_name" value="&lt;?php echo $caffe_name ?&gt;" /&gt;
              &lt;input type="hidden" name="caffe_id" id="caffe_id" value="&lt;?php  if(isset($caffe_id)) echo $caffe_id; ?&gt;" /&gt;
              &lt;div id="gll_select"&gt;
                  &lt;?php if (isset($gallery)) : ?&gt;
              &lt;select name="gll" id="gll"&gt;
          &lt;?php foreach ($gallery as $key) : ?&gt;
              &lt;option value="&lt;?php echo $key['id_gallery'] . " " . $key['name'] ?&gt;"&gt;&lt;?php echo $key['name'] ?&gt;&lt;/option&gt;
          &lt;?php endforeach; ?&gt;
              &lt;?php endif; ?&gt;
              &lt;/select&gt;
              &lt;/div&gt;
  
              &lt;?php endif ?&gt; 
  </code></pre>
  
  <p>and jquery code like this:</p>
  
  <pre><code>if ($("#caffe".length)){var cff_name = $("#caffe option:first").text(); }
      else{var cff_name = $("#caffe_name").val();}
  </code></pre>
  
  <p>Problem is following - the first statement is always true (<strong>#caffe</strong> will not exist if <code>PHP IF statement is FALSE</code>). What seems to be the problem?</p>