mak k mak k - 11 days ago 8
jQuery Question

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

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


I am getting error for this piece of code.

My document ready function is

$(document).ready( function() {

});

K48 K48
Answer

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's test it
if ($(selector).exists()) {
    // Do something
}