[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

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

0
jeremyll
Asked:
jeremyll
  • 2
2 Solutions
 
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
 
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:
Huh, wierd. I must have inadvertently fixed that when I tested the code. Well done Chris.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now