TunaFFish
TunaFFish

Reputation: 11302

javascript if - else shortcut doesn't work?

I have a checkbox couple with values "new" or "existing" company.

$("input[name=company]:checked").val()

with jQuery shows me the value of the checked box.
Now if I do:

if ($("input[name=company]:checked").val() == "new") {
    is_new_company = true;
} else { 
    is_new_company = false; 
}
alert(is_new_company);

I get the correct booleans.
Doing the shortcut:

($("input[name=company]:checked").val() == "new") ? is_new_company = true : is_new_company = false;

I get nothing.. Why is that?

Upvotes: 0

Views: 632

Answers (3)

Amarghosh
Amarghosh

Reputation: 59461

Why don't you just do:

is_new_company = $("input[name=company]:checked").val() == "new";

I tried your code on Firebug console and it works fine: (input[name=q] is the search box at the top right corner of this page)

var x;
$("input[name=q]").val() == "asd" ? x = "t r u e" : x = "f a l s e";
console.log(x);//worked as expected

By the way, I consider the following to be more readable (I'd use it only for non-boolean values, of course):

x = $("input[name=q]").val() == "asd" ? "t r u e" : "f a l s e";

Upvotes: 4

Sadat
Sadat

Reputation: 3481

is_new_company = ($("input[name='company']:checked").val() == "new") ? true : false;

Upvotes: 0

Salil
Salil

Reputation: 47522

Try

is_new_company = ($("input[name=company]:checked").val() == "new") ?  true : false;

Upvotes: 0

Related Questions