onChange texfield javascript

0

I have a textfield with an id and I want the moment I finish typing to execute a onChange action in JavaScript.

<input type='text' id='idNewClient' />

Part of JS

$("idNewClient").change(function() {
    console.log("teste");
});

I always have a " change is not a function error."

    
asked by anonymous 13.09.2018 / 18:35

2 answers

2

Your jQuery selector is wrong. A "#" is missing before the input ID.

With the following code it will work.

$("#idNewClient").change(function() {
    console.log("teste");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type='text' id='idNewClient'/>
    
13.09.2018 / 18:39
0

If the returned object does not have the method change means that this object is not a jQuery object.

To check if the $ variable is really jQuery you can check the contents of $.fn.jquery .

If a string is printed as "1.12.4" it is because $ is jQuery and the string represents the version. But this is not your case, because if it were not the error mentioned in the question would occur.

OK, we found that $ is not jQuery, the next step is to find out if jQuery was actually loaded. For this check the contents of window.jQuery . If it is undefined it is very likely that jQuery was not loaded with the page.

The only exception would be if there were some jQuery.noConflict() in your code. Example:

// window.$ não é modificado. Apenas window.jQuery contém a lib
jQuery.noConflict();

// window.$ e window.jQuery não são modificados. Apenas window.jimCarrey contém a lib
let jimCarrey = jQuery.noConflict(true);

So after understanding the above concepts the walkthrough to find out would be:

>> $.fn.jquery
"1.12.4"     // OK
>> $.fn.jquery
ReferenceError: $.fn is undefined  // $ não é jQuery

>> jQuery.fn.jquery
"1.12.4"     // OK, apenas $ não é jQuery
>> jQuery.fn.jquery
ReferenceError: jQuery is not defined  // jQuery não carregado
>> jQuery.fn.jquery
ReferenceError: jQuery.fn is undefined  // jQuery foi sobrescrito

If the above tests fail, give Ctrl + F and search for jQuery.noConflict in your code. If you do not find it, it is not loaded.

Curiosity

If jQuery is not loaded as $('body') does it return an HTML element correctly when I run it in the browser's console?!?!

Some modern browsers (such as Firefox , or Chrome and the Edge ) use $ as an alias to document.querySelector() and $$ as alias to document.querySelectorAll() cases these are not used by the page charged.

    
13.09.2018 / 20:15