1

Possible Duplicate:
How can I convert a string to boolean in JavaScript?

I have a select list with 2 options in it, yes or no, something like:

   <select size="1">
     <option value="true">yes</option>
     <option value="false">no</option>
   </select>

Now i want to use the selected value in the jquery-UI button disabled property , means :

  $("button").button({ disabled : $("select").val() });

Now my problem is that the value which we will get by $("select").val() is string and for

disabled property we need boolean. So i want to know that is there any method just like

pareInt or parseFloat by which we can convert a string to boolean ?

Community
  • 1
  • 1
Tom Rider
  • 2,597
  • 7
  • 40
  • 63
  • because of the ambiguity, you should specify what you consider true or false... By nature, all non-empty strings are true, even the string `"false"`. Do you want this to happen? be more specific please – ajax333221 Jul 04 '12 at 18:34
  • Probably you want to use – cancerbero Sep 12 '19 at 06:17

7 Answers7

5
var value = ('true' === $("select").val() );
insertusernamehere
  • 22,497
  • 8
  • 84
  • 119
1

You can use the third one:

var num = +something; //toNumber
var str = something + ""; //toString
var bol = !!something; //toBoolean

That will turn 0, "", false, null, undefined, NaN to false, and everything else to true

But using my deduction powers, you want something like "false" -> false, for this you can use one of these:

var bol = something === "true"; //false for anything different than true
var bol = something !== "false"; //true for anything different than false
ajax333221
  • 11,086
  • 15
  • 58
  • 93
0

Try with this code:

var myBool = myString == "true";
VisioN
  • 138,460
  • 30
  • 271
  • 271
Aghilas Yakoub
  • 27,871
  • 5
  • 44
  • 49
0

How about writing your own?

I'm not exactly firm in JavaScript Syntax but try this:

function a(inputString)
    if(inputString == "true")
        return true;
    if(inputString == "false")
        return false;

I'm sure there are better solutions. This one is just from the top of my head.

VisioN
  • 138,460
  • 30
  • 271
  • 271
DrEnquinox
  • 47
  • 5
0
var myBoolean = (myString === 'true') ? true : false;
micadelli
  • 2,462
  • 4
  • 24
  • 38
  • 1
    ternary operator not necessary, you can use the equality result directly: `var myBoolean = myString === 'true';` – ajax333221 Jul 04 '12 at 18:36
  • someone really -1 because my comment?, it was just a minor improvement, not really a reason to dv :/ – ajax333221 Jul 04 '12 at 18:48
  • @ajax333221 true, that yours and accepted answer are more elegant. Plus, don't I just hate anonymous downvoters. – micadelli Jul 04 '12 at 20:04
0

Something like

$("select").val() == 'true'

should do the trick.

james_bond
  • 6,668
  • 3
  • 27
  • 33
0

Depends how many times you want to do it. If its going to be littered throughout your code I would add in a function like:

Boolean.parse = function (str) {
  switch (str.toLowerCase ()) {
    case "true":
      return true;
    case "false":
      return false;
    default:
      throw new Error ("Boolean.parse: Cannot convert string to boolean.");
  }
};
rtpHarry
  • 12,690
  • 4
  • 43
  • 61