AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

HOW TO SELECT ITEM WITH CLASS WITHIN A DIV IN JQUERY

By pushpam abhishek
Listen to this article



INTRODUCTION

In this topic, we will see how to select an item with the class within a div in jquery. This is very much easy to achieve this task. we can use the basic id selector and class selector.

Select Item With Class Within A Div:-

In the following example, we will see how to select item with the class within a div. This simple HTML example has two <div> element. The outer <div> element has the id of “MyDiv“ and the inner <div> element has the class name of Select. we will select the inner <div> element and set the background color to light gray.


Select Item With Class Within A Div Example:-



<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#MyDiv').find('.Select').css('background-color','lightgray');
        });
    </script>
<body>
 
<div id="MyDiv">
<p>outer DIV</p>
    <div class="Select">
        <p>Inner Div</p>
    </div>
</div>
</body>
</html>
outer DIV
Inner Div


$(‘#MyDiv’).find(‘.Select’).find(‘p’).css(‘background-color’,’lightgray’); This line tells jquery to select the element that has the id of “MyDiv” and using jquery find function find(‘.Select’) it will select the inner element that has the. Select class and finally we have used the CSS function to set the background color for the selected <div> element. 


                                   Share this article with your friends

Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

1 comment:

  1. how to select element inside div in jquery,plz provide us Code.

    ReplyDelete