0

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
    });
});
ngplayground
  • 20,365
  • 36
  • 94
  • 173

2 Answers2

2

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
    });
});
musefan
  • 47,875
  • 21
  • 135
  • 185
1

Try this:

$('#scalecontrol').change(function(){
    map.setOptions({
        scaleControl: $(this).val() === "true"
    });
});
Renato Gama
  • 16,431
  • 12
  • 58
  • 92