Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm afraid you've misunderstood how jQuery works; your <code>if</code> statements don't make sense at all.</p> <p><code>$("[id!='']")</code> will <em>always</em> return a jQuery object which, due to JavaScript's automatic type coercion, will <em>always</em> evaluate to <code>true</code>. In other words <code>$("[id!='']") == true</code> no matter what your HTML looks like. The same applies for the rest of your <code>if</code> statements.</p> <p>Because of this mistake your code retrieves <em>both</em> the results of the UPS and FedEx scripts <em>every time</em>. Since the AJAX calls are asynchronous your results may appear in different orders thus causing an apparently random clearing of the data (when the empty data is returned <em>after</em> the correct data).</p> <p>This is how I assume you intended your code to behave:</p> <pre><code>$(document).ready(function(){ // Select all of the tracking numbers on the page $(".trackingnumber"). // Display some text in each, indicating that we're getting status text("working..."). // Begin getting status for each tracking number. each(function () { var trackingNumber = this.id; // Use the fedex script by default ... var script = "fedex"; if (trackingNumber.match(/^1z/i)) { // ... unless it's a UPS tracking number. script = "ups"; } $.post("inc/" + script + ".php", { v: trackingNumber }, function(data){ $("#" + trackingNumber).html(data); }); }); }); </code></pre> <p><strong>Working Demo:</strong> <a href="http://jsbin.com/iyeci" rel="nofollow noreferrer">http://jsbin.com/iyeci</a> (editable via <a href="http://jsbin.com/iyeci/edit" rel="nofollow noreferrer">http://jsbin.com/iyeci/edit</a>)</p> <p>Don't hesitate to ask if you need more help understanding the code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload