Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

292
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda