Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML jQuery Basics (2014) Introduction to jQuery What is jQuery?

click and onclick

HTML: <ul> <li class="0104-li">Apple</li> <li class="0104-li">Amazon</li> <li class="0104-li">Google</li> <li class="0104-li">Microsoft</li> </ul>

JavaScript using click(): $("li.0104-li").hide().parent().before("<a class='0104a' href='#'>Show Tech Giants</a>"); $("a.0104a").click(function() { $("li.0104-li").show(); });

However, when I try and change it to onclick, then it won't work: $("li.0104-li").hide().parent().before("<a class='0104a' href='#'>Show Tech Giants</a>"); $("a.0104a").onclick = function() { $("li.0104-li").show(); };

Why? Thanks for your answer.

2 Answers

Kevin Korte
Kevin Korte
28,111 Points

There is no .onclick() function in jQuery.

You either have to use .click() or the .on() function to look for clicks, like

    .on( 'click', function(){
      $('li.0104-li').show();
    });

on click is a html attribute to do something with javascript

Thanks.