Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery if two elements exists delete the first

Tags:

jquery

I want to check if two elements exist. One has the #one the other #two. And if both exists i want to remove #one.

If only #one exist do nothing. If only #two exist do nothing. If #one and #two exist delete #one.

Could someone help me to geht this thing working ?

if($("#one") AND $("#two")) {
$("#one").remove();
}

My little snippet does not wirk

like image 766
user3633186 Avatar asked Feb 13 '26 01:02

user3633186


1 Answers

You need to use && instead of AND and length to check if element exists. JQuery selector returns the object even the selector returns No element. So using .length will return zero when no element is return by selector and greater than zero when element(s) are returned by selector.

if($("#one").length && $("#two").length) {
   $("#one").remove();
}
like image 81
Adil Avatar answered Feb 16 '26 21:02

Adil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!