views:

211

answers:

2

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

I have a hidden field that contains a boolean value, I am checking if it's true or false in a javascript function, like this:

if (Trim(document.forms['mainform'].hiddenfield.value) == 'true')
{
}

which I think is a lame way to do this. I was wondering how to convert that string value into a boolean?

Thanks

A: 

This may seem a bit silly, but you could add a method to String.

String.prototype.isTrue = function() {
   return this.toLowercase() == "true"
}

gives you

>>> "true".isTrue()
true
>>> var x = "false"
>>> x.isTrue()
false

just use .isTrue() on any string literal or variable.

ithcy
A: 

A similar question was asked here.

I don't think there is anything wrong with a comparison to the string "true".

Kuzco