Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda