HOW TO SELECT ITEM WITH CLASS WITHIN A DIV IN JQUERY

HOW TO SELECT ITEM WITH CLASS WITHIN A DIV IN JQUERY,Select Item With Class Within A Div,Select Item With Class Within A Div Example,jquery
Share it:



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 it:

jQuery

Post A Comment:

1 comments:

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

    ReplyDelete