Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if an element has any children elements using jquery?

I have a div that is floating left and the other floating right. I want to check if the div that is floating right has children element; if the it don't have any visible element, I want applied and new class to the left div. See below:

<div id="leftContent" class="left ">
    <table></table>
</div> 


<div id="rightContent" class="content">
    //the dom has no visible element
    //”#ctl00_ContentPlaceHolder1_ somegridView” is not visible     
</div> 

And I’m using the following script:

$(document).ready(function() {
    if ($(“#ctl00_ContentPlaceHolder1_ somegridView”).lenght = 0) {

        $("# leftContent ").removeClass("left");
        $("# leftContent ").addClass("center");


    }
});

div.left
{
    float: left;
    width: 365px;
    margin-left: 5px;
    padding-left: 2px;
}
div.center
{
    padding: 2px;
    margin: 5px;
    float: none;
    width: 95%;
    clear: both;
}

If div id="rightContent" empty?

like image 881
Tony Avatar asked Aug 31 '09 17:08

Tony


People also ask

How do you check if an element has a child?

To check if an HTML element has child nodes, you can use the hasChildNodes() method. This method returns true if the specified node has any child nodes, otherwise false . Whitespace and comments inside a node are also considered as text and comment nodes.

How do you get children of children in jQuery?

Definition and Usage. The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

How do you get the children of the $( this selector?

Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.


1 Answers

if ( $("#rightContent").children().length > 0)
{

   // do style changes

}
like image 141
womp Avatar answered Sep 29 '22 10:09

womp