henhen henhen - 1 month ago 9
jQuery Question

If else statement for jquery using selectors to see if a certain div has been appended to it

Would this work if you had a div and you append one of these div to ? Such as

<div class ="append">
<div id ="one"></div>
<div id ="two"></div>
</div>


Then in
jQuery
do something like:

if ($(.append#one)) {do this}
else if ($(.append#two)){do this}


each of the div
#one
and
#two
have different numbers and that matters. So I was wondering if you could somehow do an if statement to check if a div has a another div with an id appended in it, if so you do the code else if its another div with another id do code.

Edit: The divs with the id one and two are not appended to div with class append at first, i have to pick either one or two to get appended to the div append. So can i check if the div .append has either div#one appended to it, if so do this, else if div#two appended to it do this

Answer

You could check if a div has a particular parent as well

if($( "#one" ).hasClass( "selected" )){
  //do something
}else if($( "#two" ).hasClass( "selected" )){
  //do something
}