2

This is my html:

<select id="popularCategorySelectID">
    <option id="1" value="false" selected="selected">No</option>
    <option id="2" value="true">Yes</option>
</select>   

And this is my javascript:

var popularCategorySelectElement = document.getElementById("popularCategorySelectID");
var popularCategorySelectElementValue = Boolean(popularCategorySelectElement.options[popularCategorySelectElement.selectedIndex].text);   

I want to get the Boolean value like true or false here in popularCategorySelectElementValue. With the above code I am always getting true. I have tried with [ngValue]="true" but the result is same true.

Thanks for your time!

Andrew
  • 373
  • 2
  • 15
Avow Studio
  • 87
  • 3
  • 8
  • Well, you're selecting the text inside each option. So you're selecting "No" or "Yes" which have no direct relation to a boolean value. Also, any non-empty string using Boolean() will return true. – Nathan Champion Jul 22 '20 at 07:39
  • You could make a function which checks the value Yes or No and returns a boolean. – Karan Kumar Jul 22 '20 at 07:40
  • I think you can find the answer in [here](https://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript). – Sett Paing Jul 22 '20 at 07:41

2 Answers2

4
let bool = document.querySelector('#popularCategorySelectID').value === "true" ? true : false

Or even simply:

// this will return true if the conditional is true 
let bool = document.querySelector('#popularCategorySelectID').value === "true"

You could get a boolean value as this.

Karan Kumar
  • 2,678
  • 5
  • 29
  • 65
0

You can simply do this :

document.getElementById("popularCategorySelectID").value
Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57