I’m totally new to jQuery trying to write some scripts to make a navigation item #printInteract trigger the container border to change colors. It felt like this should work? is this a symantics error? if not could someone maybe explain a bit where I went wrong?
jQuery:
$(document).ready(function(){
var printBorder = $("#container").css("border-color", "blue");
function changeBorder() {printBorder}
$("#printInteract").on("click", changeBorder);
});
CSS
#container{height:95%; position:relative;.clearfix(); border:.1875em #fff solid;}
HTML
<nav>
<ul class="navSpaces">
<li class="navCenter"><a class="navBg" href="#"><div class="relativeCenter"><img src="asset/img/logo30px.png" /><h3>all</h3></div></a></li>
<li id="printInteract" class="navCenter"><a class="navBg" href="#"><div class="relativeCenter"><img src="asset/img/logo30px.png" /><h3>print</h3></div></a></li>
<li class="navCenter"><a class="navBg" href="#"><div class="relativeCenter"><img src="asset/img/logo30px.png" /><h3>video</h3></div></a></li>
<li class="navCenter"><a class="navBg" href="#"><div class="relativeCenter"><img src="asset/img/logo30px.png" /><h3>web</h3></div></a></li>
</ul>
</nav>
You’re close but slightly off. I’ve commented your code to explain your mistakes.
Further, in this case, changeBorder doesn’t doo much. Namely, it only changes the border. For this case, you can just pass an anonymous function rather than define and call a function
Footnote: You can even skip the part where you grab the jquery object for your container and store it in a variable, since you only reference that object once (while changing borders). However, if you reference that object multiple times, it is considered best practice to ‘cache’ the object in a variable as the code above is doing. This is because every time you request a jQuery object, jQuery has to traverse the DOM looking for all elements that match your selector. Thus, you get marginally better performance by caching the object.
Edit:
There is a better way to do what you’re looking for without using 4 click handlers. In fact, you should certainly attempt to minimize the number of event listeners you use in a page since each takes its toll.
One way to achieve what you’re looking for is to create a Javascript object that serves as a lookup table.
See the demo here
This is the Javascript / Jquery code I used: