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

What's Wrong

Just wondering if anyone could help me? I am developing a wordpress theme and want to make the login form appear after clicking login.

Here is my code, but it doesn't seem to work.

<!-- jQuery -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script>
$("#menu-item-login").click(function () {
    $(".login").css("display", "block");
});

</script>

<!-- Nav Bar -->

<div class="menu-account-menu-container">
                    <ul id="menu-account-menu" class="menu">
                    <li id="menu-item-login"><p>Login</p></li>

    <li id="menu-item-signup"><p>Create Account</p></li>
</ul>
</div>

<!-- Login Form -->

<div class="login">
    <div class="close_login"></div>
    <form name="news_subs" method="post" action="files/subscribe.php" class="login-form">
            <ul class="login_form">
                <li><input type="text" name="email-only" id="name" placeholder="Username"></li>
                <li><input type="password" name="Password" placeholder="Password"></li>
                <li><input type="submit" name="nls_button" value="Login"></li>
            </ul>
        </form>
        </div>

3 Answers

Your JavaScript has two problems.

First, you're requesting the #menu-item-login element before it's been loaded. To fix this, you'll have to wrap your code with an anonymous function like this...

(function() {

    // your code here

})();

Second, using WordPress and jQuery together can be a little tricky. WordPress forces jQuery to let go of the $ variable for a number of reasons, so you can't use $ anymore. The only way around this is to create a new scope and assign jQuery to $ inside of that new scope.

We can make two quick edits to the code I used above to achieve this effect.

(function( $ ) {

    // your code here

})( jQuery );

And one last thing. You really don't need to use jQuery's .css( 'display', 'block' ) method to show an element. The .show() method does the exact same thing. Using .slideDown() or .fadeIn() also do the same thing, but they add some nice visual effects.

Hope that helps!

Okay thanks to you're help, I've managed to get the following working, but this has the $

 $( document ).ready(function() {
    $("#menu-item-login").click(function () {
    $(".signup").slideUp();
    $(".login").slideDown();
});
  $(".close_login").click(function () {
    $(".login").slideUp();
});
    $("#menu-item-signup").click(function () {
    $(".login").slideUp();  
    $(".signup").slideDown();
});
  $(".close_signup").click(function () {
    $(".signup").slideUp();
});
    });

But this doesn't?

(function( $ ) {
$("#menu-item-login").click(function () {
    $(".signup").slideUp();
    $(".login").slideDown();
});
  $(".close_login").click(function () {
    $(".login").slideUp();
});
    $("#menu-item-signup").click(function () {
    $(".login").slideUp();  
    $(".signup").slideDown();
});
  $(".close_signup").click(function () {
    $(".signup").slideUp();
});
})( jQuery );

Why? What have I done wrong?

I'm not sure, but I think this is what you're asking. I'll do my best to explain, but you'd be a lot better off if you took the JavaScript Foundations course.

Basically, whenever you create a new function, you create a new level of scope.

var foo = 123;

function bar() {
    var foo = 456;
}

In the example above, the two foo variables are completely separate from each other. They're two entirely different things, even though they have the same name.

(function( $ ) {

    // your code here

})( jQuery );

Basically, by using this code above, you created an anonymous function that takes an argument set to $. After the function was created, it was immediately run, passing in the jQuery variable. Now jQuery is available inside that function under the alias of $.

Outside of the function, however, the $ variable doesn't exist, because it's just an alias for that particular anonymous function. It is invisible to the outside world.

Again, you'd be much better serviced if you'd take the JavaScript Foundations course. Jim Hoskins does a much better job of explaining this than I do.

Ah I get it now :) Thanks for everything Ryan.

Ah I get it now :) Thanks for everything Ryan.