title: Preventdefault() function on an anchor working randomly
body: <p>I have a e-commerce site I'm working on with sessions, I need to prevent default on a href att on a "a" tag when one of the 2 sessions is empty. I managed to do it but it will only fail the first time I add ONLY 1 element to the session in question. To clarify, if I add ONLY 1 element to my "cart" it will still preventdefault, but if I add 2 or more, or remove them in any way or numbers it will work properly preventing only if the cart is empty.</p>
  
  <p>As my site refresh the page to submit the data to my cart session I created a variable called data that will be "1" if the session isn't empty and "2" if session is empty, then I echo the variable on a hidden input with id data and use jquery to check the value of the element data and prevent default when it is different than 2.</p>
  
  <p>// here I define the variable data //</p>
  
  <pre><code>&lt;?php 
  if(!isset($_SESSION_["shopping_cart"])){
  $data = "1";
  }else{
  $data = "2";
  }
  ?&gt;
  </code></pre>
  
  <p>//this is my anchor//</p>
  
  <pre><code>  &lt;li&gt;&lt;a id="checkout" href="/bubale/checkout.php" 
    onclick="check();"&gt;CHECKOUT&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>// here I insert $data into a hidden input //</p>
  
  <pre><code>   &lt;input type="hidden" id="data" value="&lt;?php echo $data; ?&gt;"&gt;
  </code></pre>
  
  <p>// here if my script //</p>
  
  <pre><code>   &lt;script type="text/javascript"&gt;
  
  
  $( "#checkout" ).click(function( event ) {
      if ($('#data').val() != "2") {
     event.preventDefault();
     alert("Seu carrinho de compras esta vacio");
  }
  
      });
  
  
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p>It should work just fine BUT for some reason the first element added to the cart will still prevent default, my guess is that $data is not changing for some reason.</p>
  
  <p>Any help will be appriciated, even if you have a different way to do this I will be glad to get feedback on this...</p>