jeudi 8 octobre 2020

How to do an if statment in JavaScript? [closed]

I have no experience with js I have manage to do this:

$('#comparemodel').on('change', function () {
  var outputcompare = document.getElementById('ouputcompare');
  var valuesarray = $('#comparemodels').val();
  if (valuesarray.length > 1) {
    var bettermodelvalue = Math.min.apply(null,valuesarray);
    outputcompare.innerHTML = bettermodelvalue;
  }else{}

But doesn't work an if I change the condition to: e.g if(valuesarray.length == 2) it does work but of course only if I have 2 options selected.

Why is this happening? How can I fix it?

The goal of the code is to print the minimal value from select's optionsoptions:

<div>
   <div class = "form-group">
       <label class = "control-label" for="comparemodels">Compare Models:</label>
       <select data-title="Select at least two models" id = "comparemodels" class = "selectpicker form-control">
            <option value = 1>Model 1</option>
            <option value = 2>Model 2</option>
            <option value = 3>Model 3</option>
            <option value = 4>Model 4</option>
        </select>
    </div>
    <div>
         <p id= "outputcompare"></p>
    </div>
</div>

Aucun commentaire:

Enregistrer un commentaire