Is it good practice to check that element exist before remove it or it is not necessary ?
For example:
if(('#el').length > 0) {
$('#el').remove();
}
is same as
$('#el').remove();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
All jQuery selectors return a collection (perhaps empty) of jQuery objects and so if
$('#el')(or more likely$('.class')) returned a number of objectsremove()would remove them all. Likewise, if your selector doesn’t return any objectsremove()will not remove any.So no. It isn’t necessary.