-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>
5

1 Answer 1

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 :)

Sign up to request clarification or add additional context in comments.

6 Comments

can you show me how to do it ? I'm sorry I new in js :')
Try printing to the log the selected entry what datepicker is and see how it can be accessed accurately.
i don't know what you say, im sorry. where I get it ?
Add this line at the beginning of the function: console.log ('numberdays.value: ',numberdays.value) and check with the developer tools in the console what the value is written there, by this you will know where the error is.
it looks like i have a problem in my datepicker js. When I input and look at the console, nothing happens. the result is NULL
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.