1

I've created an input and a button. When you press the button, if you put one of 3 states in it makes certain panels appear and disappear. This part works fine. The only problem is that it only accepts the input if you do a lowercase nj.

How can I change my code to accept both lowercase, upper case, and the word New Jersey spelled out in full?

var stateSearch, searchButton, nj, pa
nj = "nj";
stateSearch = $("#stateSearch").val();

if (stateSearch === nj) {
    $(".nj").fadeIn(2000);
    $(".pa").fadeOut(500);
    $(".ny").fadeOut(500);
    return;

2 Answers 2

2

Make a dictionary to accept these different versions

var acceptState = {
    "NJ" : true,
    "NEW JERSEY" : true
};

And then use toUpperCase when you check this dictionary

if (acceptState[stateSearch.toUpperCase()]) {
Sign up to request clarification or add additional context in comments.

Comments

2

You can force the user input to lower case to match the string you're testing against by using toLowerCase(). Try this:

if (stateSearch.toLowerCase() === nj) {
    // your code...
}

Comments

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.