Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
why is difference between jquery input element and javascript input element?

I have two type of call function check_negative_value(). in first type I set the onchange=change_quantity(this)and then call check_negative_value() from there like:

function change_quantity(elem) {
    check_negative_value(elem);
    console.log(elem);
}

and in second type I set the decrement_cart(this) and then call check_negative_value() from there like:

function decrement_cart(elem) {
    const input = $(elem).next('input');
    check_negative_value(input);
    console.log(input);
}

and in check_negative_value() I must use input.value or input.val() !!

function check_negative_value(input){
    if(input.value <= 0){
        alert('not accept value under 1');
        input.value = 1;
    }
}

Or :

function check_negative_value(input){
    if(input.val() <= 0){
        alert('not accept value under 1');
        input.val(1);
    }
}

because each calling function have different element and the result of console.log is this: in jquery call:

r.fn.init [input.input-number.text-center, prevObject: r.fn.init(1)]

in javascript call:

<input class="input-number text-center" type="number" value="-1" min="0" max="1000" onchange="change_quantity(this)">

the html code is :

<div class="input-group-button" onclick="decrement_cart(this)">
    <span class="input-number-decrement">-</span>
</div>
<input class="input-number text-center" type="number" min="0" max="1000"
                                    onchange="change_quantity(this)">
<div class="input-group-button" onclick="increment_cart(this)">
    <span class="input-number-increment">+</span>
</div>```

how can I use input element in check_negative_value() without difference between input.val() and input.value ???

thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Neither JavaScript nor jQuery has an input element. input elements are DOM objects provided by the host environment (browser), not the language (JavaScript) or library (jQuery).

The difference you're seeing is the DOM object (with a value property) vs. a jQuery wrapper object around it (with a val method).

how can I use input element in check_negative_value() without difference between input.val() and input.value ???

You can check to see whether what you have is a jQuery wrapper by seeing if it has a jquery property or perhaps more directly in this case if it has a val property:

function check_negative_value(input){
    if (input.jquery) {
        // It's a jQuery wrapper, get the first DOM element it contains from it
        input = input[0];
    }
    if (input.value <= 0){
        alert('not accept value under 1');
        input.value = 1;
    }
}

Side note: val() returns a string (unless the jQuery set is empty), not a number (the value of value is also always a string). Although <= 0 will implicitly convert it, you might consider converting it on purpose as implicit conversion assumes "" should be 0; more in my answer here.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!