PHP isset analog in jQuery


PHP has a useful function isset(), with which you can check whether a variable exists so as not to get errors.
But there is no such function in JavaScript, but sometimes, you need to check whether an element exists on the page, and not to catch an error during the processing of the script.

To do this, you can use the jQuery library and the .length property. Using .length you can search to see if an element exists on the page, and if it exists, you can continue to execute the code.

if(jQuery('.element').length > 0){ // If the property returns 0, then the element does not exist, and the code will not be executed.
	jQuery('.element').addClass('active');
}



Connect

If you have any questions, want to get individual advice or you have an interesting offer, you can contact via the feedback form.

Or write to the email info@bobreod.ru





By submitting the form, you agree to our privacy policy