Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
How to select first child in jquery in Jquery functions of JQuery Examples
3131 Views
Hints 
.first() used to select the first element from the children which have the same class name.In the following example, when you click the click here first child will change into red color.
Example code
<html>

<head>
    <title>HTML-JQuery</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

    <script type="text/javascript">
        $(function () {
            $("div.exam").click(function () {
                $("div.exam div.num").first().css("color", "red");
            });
        });
    </script>
</head>

<body>
    <div class="exam">Click here to apply css red color to first element
        <div class="num">first</div>
        <div class="num">second</div>
        <div class="num">third</div>
        <div class="num">fourth</div>
        <div class="num">last</div>
    </div>
</body>

</html>
Demo 
Previous Topics
Previous lessons of current book.
Best Lessons of "JQuery Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details