JQuery if two elements exist, remove the first
I want to check if two items exist. One has #one another #two. And if both exist, I want to remove #one.
Unless #one does anything at all. If only #two exist, do nothing. If #one and #two exist delete #one.
Can anyone help me get this job done?
if($("#one") AND $("#two")) {
$("#one").remove();
}
My little piece is not wirk
+3
source to share
2 answers
You need to use and &&
instead of to check if the element exists. The jQuery selector returns an object, even the selector returns an item None . Thus, using .length will return zero if no element is returned by the selector and is greater than zero when an element is returned by the selector.AND
length
if($("#one").length && $("#two").length) {
$("#one").remove();
}
+6
source to share