Uncaught TypeError: j $ (...). Exists is not a function

var selector = '#Id-value_' + index; var exist = $(selector).exists(); 

I get an error for this piece of code.
My document function is ready

 $(document).ready( function() { }); 
+5
source share
2 answers

There is no exists() function in jQuery. But you can quickly write one:

 //add a new function to jQuery jQuery.fn.exists = function(){return this.length>0;} //now let test it if ($(selector).exists()) { // Do something } 
+6
source

Based on @ K48's answer, you can also directly check if it exists with $(selector).length .

 var selector = '#Id-value_' + index; if ($(selector).length) { // Do something } 
+3
source

Source: https://habr.com/ru/post/1260567/


All Articles