How To How to search child element in a div or any element by JQuery has()

How to search child element in a div or any element by JQuery has()

How to search child element in a div or any element

Hello Readers.
This article is about how you can find any element within an element. For example, if you want to find any element or search a child element with a particular id or class using jQuery to know whether it exists or not, user the following method. Be sure, its very very easy. So let’s have a look on the code.

To search a child elements there are many methods available in jQuery, but in this case what I found the most suitable is this method which worked best for me. I have also used other methods like find() etc. those can also be used to find the child element as well but if we use find() , we need to be aware of the exact position of the div or element.  Sometimes it does not provide the required results like in the case below that is why i have used has() function. has() function tells whether the div or elements has the element we are finding or not. So both methods are good however usefulness depends on the requirements.

First i am creating html in which i am taking card div by bootstrap and putting two different buttons in it. One is  in inside the div.card-body (div which has class=”card-body”). Therefore, saying elements anywhere inside that class are inside card body and others outside that class are termed as outside card body.

Now we have Two buttons with class=”find-one”, by clicking on them we can find whether the button is inside div or outside it.

The result test describing whether the element is inside div or not is taken in a separate heading tag having class=”result’.

The complete code to search a child element in a div by jQuery is:

You can download this code here

Download

4 thoughts on “How to search child element in a div or any element by JQuery has()

  1. Hey this is kinda of off topic but I was wondering if
    blogs use WYSIWYG editors or if you have to
    manually code with HTML. I’m starting a blog soon but have no
    coding expertise so I wanted to get guidance from someone
    with experience. Any help would be greatly appreciated!

  2. A person necessarily assist to make severely articles I’d state.
    That is the very first time I frequented your website page and
    to this point? I surprised with the analysis you made to create this particular publish amazing.
    Fantastic activity!

Leave a Reply