Jquery: remove div X if div Y does not exist

I would like to use Jquery to remove div X if div Y does not exist

The jquery below doesnt' work,

$(function () {
if ($("#ctl03_LeftNav").length == 0) {
        $("#left_container").remove();
)
});


<div id="content_container">
            <div id="left_container">
              <!--  <div id="ctl03_LeftNav">-->
                <!-- Empty white space -->



                <!--</div>-->
            </div>
            <div class="right_container" id="ctl03_right_container">
                <div class="content">
                </div>               
            </div>
        </div>

Open in new window

jeremyllAsked:
Who is Participating?
 
Chris StanyonCommented:
You have a typo in your code.

Fourth line of your code should be a bracket, not a parenthesis - should be } - not )



$(function () {
   if ($("#ctl03_LeftNav").length == 0) {
      $("#left_container").remove();
   } //this should be a curly bracket!
});

Open in new window

0
 
ChrisCommented:
Your code should work. I've created a test page using your code which demonstrates it working.

Clicking 'Run Div Check' runs the code you've provided. Clicking 'Remove Second Div' removes the inner Div which you're checking the existance of.

As you can see, your code seems to work fine.
test.htm
0
 
ChrisCommented:
Huh, wierd. I must have inadvertently fixed that when I tested the code. Well done Chris.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.