-1

I don't understand why when I use the condition value like a > b but it doesn't work properly, maybe because of the value a = decimal. following my code:

HTML

<input type="text" class="form-control" name="numberdays"  id="numberdays" value="10.0/>
<input type="text" name="cutii" id="cutii" value="9.0">
<button class="btn btn-primary waves-effect" id="subcut" type="submit" disabled>

SCRIPT

cutifrom.addEventListener('input',()=>{
            if (cutii.value > numberdays.value) {
            subcut.removeAttribute('disabled');
        }else{
            subcut.setAttribute('disabled','disabled');
        }
        }) ;  

the result is that my button is disabled, it shouldn't be.

here's my js. actually number days I use the datepicker and generate numbers that are automatically generated. maybe because it's the condition that I use the operator is not detected. JS for datepicker

 <script type="text/javascript">
      $(document).ready(function(){

    let $fromDate = $('#fromdate'),
        $toDate = $('#todate');

    $fromDate.datepicker().on('change', function(){
        $toDate.datepicker('option', 'minDate', $(this).val());
    });

    $toDate.datepicker().on('change', function(){
        $fromDate.datepicker('option', 'maxDate', $(this).val());
    });;
});

      $(function() {
    let $fromDate = $('#fromdate'),
        $toDate = $('#todate'),
        $numberDays = $('#numberdays'),
        $numberCuti = $('#cuti');

    $fromDate.datepicker().on('change', function(){
        $toDate.datepicker('option', 'minDate', $(this).val());

        $numberDays.val(calculateDateDiff($toDate.val(), $(this).val()));
    });

    $toDate.datepicker().on('change', function(){
        $fromDate.datepicker('option', 'maxDate', $(this).val());

        $numberDays.val(calculateDateDiff($(this).val(), $fromDate.val()));
    });

   cutifrom.addEventListener('input',()=>{
        if (parseFloat(cuti.value) >= parseFloat(numberdays.value)) {
        subcut.removeAttribute('disabled');
    }else{
        subcut.setAttribute('disabled','disabled');
    }
    }) ;     
        
function calculateDateDiff(endDate, startDate) {
        if (endDate && startDate) {
            let e = moment(endDate),
                s = moment(startDate);

            return e.diff(s, "days");
        }

        return null;
    }   
});
</script>
Diyan Knd
  • 21
  • 1
  • 9
  • https://stackoverflow.com/questions/21690070/javascript-float-comparison – bassxzero Oct 23 '20 at 03:05
  • 1
    [Duplicate](https://www.google.com/search?q=site%3Astackoverflow.com+js+compare+numbers+from+inputs) of [Using Javascript to compare two input numbers in HTML?](https://stackoverflow.com/q/15914514/4642212). Also read [Input value is a string instead of a number](https://stackoverflow.com/q/27849944/4642212) and [Javascript to convert string to number?](https://stackoverflow.com/q/2130454/4642212). – Sebastian Simon Oct 23 '20 at 03:16
  • what if the value of ```numberdays``` is automatically input from js datepicker ? – Diyan Knd Oct 23 '20 at 03:31
  • if i input manual in ```numberdays``` its working . but if I'm using datepicker for value on my ```numberdays``` its not working – Diyan Knd Oct 23 '20 at 03:32
  • Does this answer your question? [Javascript float comparison](https://stackoverflow.com/questions/21690070/javascript-float-comparison) – Martin Brisiak Oct 23 '20 at 09:09

1 Answers1

3

Your value is a String type and the comparison is not like Number, try converting the value to a number and see if it works.

You need to change the code to this form:

  cutifrom.addEventListener('input',()=>{
        if (Number(cutii.value) > Number(numberdays.value)) {
        subcut.removeAttribute('disabled');
    }else{
        subcut.setAttribute('disabled','disabled');
    }
    }) ; 

Good Luck :)

semicolon
  • 255
  • 1
  • 3
  • 11