Home > OS >  HTML option value post over Boolean instead of String
HTML option value post over Boolean instead of String

Time:10-26

I have a simple HTML select dropwdown in which the user can change between the following values

<select id="changeIsActive">
    <option value = true>Active</option>
    <option value = false>Inactive</option>
    <option>All</option>
</select>

I then have an event that binds to ^ that "converts" the String values into actual Booleans. And the Booleans then get passed to a Mongo Query Object for the Server to filter on.

'change #changeIsActive'(e, template) {           
          let retVal;
          switch (e.target.value) {
            case "true":
                retVal = true;
                break;
            case "false":
                retVal = false;
                break;
            case "All":
                retVal = null;
            default:
                break;
        }
    }

Is there a better way of converting the String literal of "true" and "false" to their Boolean values? Thanks

CodePudding user response:

'change #changeIsActive'(e, template) {           
const obj = {
    'true':true,
    'false':false,
    'All':null
}

const retVal  = obj[e.target.value] 
}
  • Related