Bryan Veloso
Bryan Veloso

Reputation: 25

How to make this jquery var shorter?

How to make this variable shorter?

var outskirts = ((jQuery("#billing\\:city").val() == "Manila")||(jQuery("#billing\\:city").val() == "Quezon City"))

I already tried:

var outskirts = ((jQuery("#billing\\:city").val() == "Manila" || "Quezon City"))

And I also tried:

var outskirts = (jQuery("#billing\\:city").val() == ("Manila") || ("Quezon City"))

But it doesn't seem to work.

Upvotes: 1

Views: 42

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371049

Save the value in a variable first.

const val = jQuery("#billing\\:city").val();
const outskirts = val === "Manila" || val === "Quezon City";

In most cases, jQuery will be aliased to $, so the following will probably work too:

const val = $("#billing\\:city").val();

You could also do

const outskirts = ['Manila', 'Quezon City'].includes($("#billing\\:city").val());

Upvotes: 2

Related Questions