Solved

jQuery: Make $(this) apply ONLY to the last child clicked

Posted on 2014-02-16
2
320 Views
Last Modified: 2014-02-16
How can I get $(this) to ONLY apply to the last child clicked on?  For example, if I click on "Kale" only Kale should become red and not all parent <li> tags.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Examples</title>
<style type="text/css">
li {
cursor: pointer;
}

</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
 $('li').click(function (e) {
        $('li').css('background','transparent');
        $(this).css('background','red');
 });
});

</script>

</head>
<body>

<ul>
    <li>Animals
     <ul>
       <li>Mammals</li>
       <li>Reptiles</li>
       <li>Fish</li>
     </ul>
    </li>
    <li>Minerals
     <ul>
       <li>Salt</li>
     </ul>
    </li>
    <li>Vegitation
        <ul>
            <li>Fruit
                <ul>
                    <li>Apple</li>
                    <li>Peach</li>
                    <li>Pear</li>
                </ul>
            </li>
            <li>Vegies
                <ul>
                    <li>Leafy
                         <ul>
                             <li>Kale</li>
                             <li>Spinich</li>
                             <li>Lettuce</li>
                        </ul>
                    </li>
                    <li>Roots
                         <ul>
                             <li>Kale</li>
                             <li>Spinich</li>
                             <li>Lettuce</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>


</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
$(document).ready(function () {
 $('li li').click(function (e) {
        $('li').css('background','transparent');
        $(this).css('background','red');
 });
});

Open in new window

0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
I think the proper way would be to call stopPropagation() in your event handler:
$(document).ready(function () {
 $('li').click(function (e) {
        e.stopPropagation();
        $('li').css('background','transparent');
        $(this).css('background','red');
 });
});

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now