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
Aled Evans
3,163 PointsWhat'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
Ryan Duchene
Courses Plus Student 46,022 PointsYour 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!
Aled Evans
3,163 PointsAh I get it now :) Thanks for everything Ryan.
Aled Evans
3,163 PointsAh I get it now :) Thanks for everything Ryan.
Aled Evans
3,163 PointsAled Evans
3,163 PointsOkay thanks to you're help, I've managed to get the following working, but this has the $
But this doesn't?
Why? What have I done wrong?
Ryan Duchene
Courses Plus Student 46,022 PointsRyan Duchene
Courses Plus Student 46,022 PointsI'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.
In the example above, the two
foovariables are completely separate from each other. They're two entirely different things, even though they have the same name.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.