0

I am trying to pull store locations using conditional statements. When I search for Dealer, Wholesale, or retail respectively from select option menu, the results are displayed. However, I cannot display all the location if All is selected. If I remove &&(locationData['category']=== 'Dealer' || 'Wholesale' ||'Retail'), I can show All the locations, but I can't display store locations based on each category. How can I set up a conditional statement?

This is what I have in HTML:

<select id="category" name="category">
    <option value="" selected>All</option>
    <option value="Dealer">Wholesale &amp;Retail</option>
    <option value="Wholesale">Wholesale</option>
    <option value="Retail">Retail</option>
</select>

This is what I have in js

if(settings.maxDistance === true && firstRun !== true){ 
    if((locationData['distance'] < maxDistance)&&(locationData['category']=== 'Dealer' || 'Wholesale' ||'Retail')) {
       locationset[i] = locationData;
    } else {
       return;
     }
    } else {
       locationset[i] = locationData;
    }
    i++;
    });
}
1
  • Show us a piece of jQuery in question. Commented Nov 13, 2013 at 4:30

1 Answer 1

1

I'm not entirely sure what question you're asking so I'll answer several things that are going on in your question.

If you want to know how to get the value from the select object, you can do that like this:

$("#category").val()

If you want to know how to detect the "All" choice, then you should change your HTML to this (set a value for the "All" item):

<select id="category" name="category">
     <option value="All" selected>All</option>
     <option value="Dealer">Wholesale &amp;Retail</option>
     <option value="Wholesale">Wholesale</option>
     <option value="Retail">Retail</option>
</select>

And you can check for it with a conditional like this:

if ($("#category").val() === "All") {
    // All is selected
}

I also see in your code that you have an error in your conditional. You can't compare to multiple things like this:

&&(locationData['category']=== 'Dealer' || 'Wholesale' ||'Retail')

You have to compare to each one separately:

&& (locationData['category']=== 'Dealer' || locationData['category'] === 'Wholesale' || locationData['category'] === 'Retail')

Or, using a temporary variable to hold the comparison value, your code could look like this:

if (settings.maxDistance && !firstRun) { 
    var item = locationData['category'];
    if((locationData['distance'] < maxDistance) && (item === 'Dealer' || item === 'Wholesale' || item === 'Retail')) {
        locationset[i] = locationData;
    } else {
        return;
    }
} else {
    locationset[i] = locationData;
}

If you do this a lot, sometimes, it's useful to make a static object that you can use as a lookup table:

var categories = {Dealer: true, Wholesale: true, Retail: true};
if (settings.maxDistance && !firstRun) { 
    if((locationData['distance'] < maxDistance) && (categories.hasOwnProperty(locationData['category'])) {
        locationset[i] = locationData;
    } else {
        return;
    }
} else {
    locationset[i] = locationData;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help! I haven't noticed you'd already responded my question. I joined this forum 2 days ago, I've kept waiting for email notification. Sorry.. If I remove (item === 'Dealer' || item === 'Wholesale' || item === 'Retail')), I can display each category, but I cannot show all the location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.