title: jquery - return value using ajax result on success
body: <p>I have a small problem with jQuery <code>$.ajax()</code> function.</p>
  <p>I have a form  where every click on the radio button or selection from the dropdown menu creates a session variable with the selected value.</p>
  <p>Now - I have one of the dropdown menus which have 4 options - the first one (with label None) has a <code>value=&quot;&quot;</code> other have their ids.</p>
  <p>What I want to happen is to None option (with blank value) to remove the session and other to create one, but only if session with this specific select name doesn't already exist - as all other options have the same amount assigned to it - it's just indicating which one was selected.</p>
  <p>I'm not sure if that makes sense - but have a look at the code - perhaps this will make it clearer:</p>
  <pre><code>$(&quot;#add_ons select&quot;).change(function() {
          // get current price of the addons
          var order_price_addon = $(&quot;.order_price_addon&quot;).text();
          // get price of the clicked radio button from the rel attribute
          var add = $(this).children('option').attr('label');
          var name = $(this).attr('name');
          var val = $(this).val();
          
          
          if(val == &quot;&quot;) {
              var price = parseInt(order_price_addon) - parseInt(add);
              removeSession(name);
          } else {
              if(isSession(name) == 0) {
                  var price = parseInt(order_price_addon) + parseInt(add);
              }   
              setSession(name, val);              
          }
          
          $(&quot;.order_price_addon&quot;).html(price);    
          setSession('order_price_addon', price);         
          updateTotal();
  });
  </code></pre>
  <p>so - first of all when the <code>#add_ons</code> select menu triggers &quot;change&quot; we get some values from a few elements for calculations.</p>
  <p>we get the label attribute of the option from our select which stores the value to be added to the total, name of the select to create session with this name and value to later check which one was selected.</p>
  <p>now - we check whether the <code>val == &quot;&quot;</code> (which would indicate that None option has been selected) and we deduct the amount from the total as well as remove the session with the select's name.</p>
  <p>After this is where the problem starts - <code>else</code> statement.</p>
  <p>Else - we want to check whether the <code>isSession()</code> function with the name of our selector returns 0 or 1 - if it returns 0  then we add to the total the value stored in the label attribute, but if it returns 1 - that would suggest that session already exists - then we only change the value of this session by recreating it - but the amount isn't added to it.</p>
  <p>Now <code>isSession</code> function looks like this:</p>
  <pre><code>function isSession(selector) {
      $.ajax({
          type: &quot;POST&quot;,
          url: '/order.html',
          data: ({ issession : 1, selector: selector }),
          dataType: &quot;html&quot;,
          success: function(data) {
              return data;
          },
          error: function() {
              alert('Error occured');
          }
      });
  }
  </code></pre>
  <p>Now - the problem is - that I don't know whether using <code>return</code> will return the result from the function - as it doesn't seem to work - however, if I put the &quot;data&quot; in the success: section into the <code>alert()</code> - it does seem to return the right value.</p>
  <p>Does anyone know how to return the value from the function and then compare it in the next statement?</p>
  <hr />
  <p>Thanks guys - I've tried it in the following way:</p>
  <pre><code>function isSession(selector) {
      $.ajax({
          type: &quot;POST&quot;,
          url: '/order.html',
          data: ({ issession : 1, selector: selector }),
          dataType: &quot;html&quot;,
          success: function(data) {
              updateResult(data);
          },
          error: function() {
              alert('Error occured');
          }
      });
  }
  </code></pre>
  <p>then the <code>updateResult()</code> function:</p>
  <pre><code>function updateResult(data) {
      result = data;
  }
  </code></pre>
  <p><code>result</code> - is the global variable - which I'm then trying to read:</p>
  <pre><code>$(&quot;#add_ons select&quot;).change(function() {
          // get current price of the addons
          var order_price_addon = $(&quot;.order_price_addon&quot;).text();
          // get price of the clicked radio button from the rel attribute
          var add = $(this).children('option').attr('label');
          var name = $(this).attr('name');
          var val = $(this).val();
          
          
          if(val == &quot;&quot;) {
              var price = parseInt(order_price_addon) - parseInt(add);
              removeSession(name);
          } else {
              isSession(name);
              if(result == 0) {
                  var price = parseInt(order_price_addon) + parseInt(add);
              }   
              setSession(name, val);              
          }
          
          $(&quot;.order_price_addon&quot;).html(price);    
          setSession('order_price_addon', price);         
          updateTotal();
      });
  </code></pre>
  <p>but for some reason - it doesn't work - any idea?</p>