ngplayground
ngplayground

Reputation: 21627

Javascript String to Boolean

At the moment I'm using jQuery to check the value of the select box and the values are either true or false but when I go to select the value in the jquery it recognises it as a string.

Then I have to go change it to a Boolean for it to work in my setOptions I was wondering if it was possible to convert to Boolean without having to go through the process I've done below?

html

<select name="scalecontrol" id="scalecontrol">
    <option value="false">None</option>
    <option value="true" selected="selected">Standard</option>
</select>

jQuery

$('#scalecontrol').change(function(){
    ao.scalecontrol = $(this).val();
    if (ao.scalecontrol == 'false'){
        ao.scalecontrol = false;
    } else {
        ao.scalecontrol = true;
    }
    map.setOptions({
        scaleControl:ao.scalecontrol
    });
});

Upvotes: 0

Views: 164

Answers (2)

Renato Gama
Renato Gama

Reputation: 16519

Try this:

$('#scalecontrol').change(function(){
    map.setOptions({
        scaleControl: $(this).val() === "true"
    });
});

Upvotes: 1

musefan
musefan

Reputation: 48415

If using .val() results in a string type then you have to work with that, however you can shorten your code to the following:

$('#scalecontrol').change(function(){
    ao.scalecontrol = $(this).val() == 'true';
    map.setOptions({
        scaleControl:ao.scalecontrol
    });
});

Upvotes: 2

Related Questions