Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 1

Jesus Bayona
Jesus Bayona
4,937 Points

How do I know what element of the HTML to select? Is there absolutely only one way?

Assuming that there are no complications in the future of this program, would I be able to access the same part of the HTML by using this code: '$("body div ul li")' or some similar variation instead of '$(".controls li")'? I see that the speaker's way is easier, but just wondering how this works.

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

Maybe, it would work, but later on in the page if you had set of list items inside an unordered list, inside a div, inside of the body, which could be a very high possibility (think navigation), than this jquery is going to bind to that as well, unintentionally.

So technically yes, but you'd never want to use such generic selectors because of the problems it could impose.

jason chan
jason chan
31,009 Points
#use id tags
or use
.class tags

//you mostly .addClass or .removeClass on certain events like on
<h1>learning js</h1>
    <p>js is: </p>
    <ul id="optionLIst">
        <li>a language that runs in browser</li>
        <li>simple, but powerful</li>
        <li>cool stuff</li>
    </ul>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    var id = document.getElementById("optionList")
    var myListItems = document.getElementsByTagName("li")
    $( "li" ).on( "click", function() {
    $( this ).css( "background-color", "red" );
    });
    $( "li" ).dblclick(function(){
        $(this).css("background", "white");
    })
    </script>

Anyways enjoy i made simple example above. https://api.jquery.com/hover/

Mostly click, hover, double click, on load.

So select class or id on event do something add or remove css rule. That's like most jobs already. LOLs.