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

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,148 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.