I think that I should step back a bit, and explain with more detail how I came up with the initial code in my first post.
The code from the original poster (OP) is a disaster. Here’s what it looks like:
$(document).ready(function() {
$('#showmenu').click(function() {
$('.menu').show("slide");
});
});
jQuery(document).ready(function (e) {
function t(t) {
e(t).bind("click", function (t) {
t.preventDefault();
e(this).parent().fadeOut()
})
}
e(".dropdown-toggle").click(function () {
var t = e(this).parents(".button-dropdown").children(".dropdown-menu").is(":hidden");
e(".button-dropdown .dropdown-menu").hide();
e(".button-dropdown .dropdown-toggle").removeClass("active");
if (t) {
e(this).parents(".button-dropdown").children(".dropdown-menu").toggle().parents(".button-dropdown").children(".dropdown-toggle").addClass("active")
}
});
e(document).bind("click", function (t) {
var n = e(t.target);
if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-menu").hide();
});
e(document).bind("click", function (t) {
var n = e(t.target);
if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-toggle").removeClass("active");
})
});
$(function () {
$('input[type="checkbox"]').click(
function () {
value = $(this).val();
if ($(this).is(':checked')) {
$('<li></li>').appendTo('#div ol').text($(this).val());
}
else {
value = $(this).val();
if ($('#div ol').has('li:contains("'+value+'")')) {
$('#div ol li:contains("'+value+'")').remove();
}
}
});
});
I find that JSLint is really effective at helping you to steer clear of bad code.
Pasting the code in to JSLint gives us a lot of beneficial feedback on how to improve this code.
Here are the warnings that it gives, and what I’ve done to solve them.
Use double quotes, not single quotes.
Replace single quotes for delimiting strings with double quotes.
As an example, I’m commenting out the previous line of code, and showing the updated line beneath it.
// $('#showmenu').click(function() {
$("#showmenu").click(function() {
The following code was more difficult to update:
$('input[type="checkbox"]').click(
Fortunately though the attribute equals documentation has a nice and clear solution for us. It shows examples that use double quotes outside, and single quotes inside. For example:
jQuery( "[attribute='value']" )
So we can use that same formatting style to fix our problem:
// $('input[type="checkbox"]').click(
$("input[type='checkbox']").click(
And now that double quote problem is fixed.
Use spaces, not tabs.
The next issue is all about formatting. While it is possible to fix these things manually, being commanded by JSLint about every little detail, we can use the JavaScript Beautifier to automatically fix all of these formatting problems for us.
That now gives us the following code with improved formatting and indenting, and JSLint now has no more problems with the formatting of the code.
$(document).ready(function() {
$("#showmenu").click(function() {
$(".menu").show("slide");
});
});
jQuery(document).ready(function(e) {
function t(t) {
e(t).bind("click", function(t) {
t.preventDefault();
e(this).parent().fadeOut()
})
}
e(".dropdown-toggle").click(function() {
var t = e(this).parents(".button-dropdown").children(".dropdown-menu").is(":hidden");
e(".button-dropdown .dropdown-menu").hide();
e(".button-dropdown .dropdown-toggle").removeClass("active");
if (t) {
e(this).parents(".button-dropdown").children(".dropdown-menu").toggle().parents(".button-dropdown").children(".dropdown-toggle").addClass("active")
}
});
e(document).bind("click", function(t) {
var n = e(t.target);
if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-menu").hide();
});
e(document).bind("click", function(t) {
var n = e(t.target);
if (!n.parents().hasClass("button-dropdown")) e(".button-dropdown .dropdown-toggle").removeClass("active");
})
});
$(function() {
$("input[type="
checkbox "]").click(
function() {
value = $(this).val();
if ($(this).is(":checked")) {
$("<li></li>").appendTo("#div ol").text($(this).val());
} else {
value = $(this).val();
if ($("#div ol").has("li:contains(" + value + ")")) {
$("#div ol li:contains(" + value + ")").remove();
}
}
});
});
Unexpected ‘this’.
The this keyword easily leads to many confusions and misunderstandings. There are better ways than using the this.keyword.
Here’s the section of code that has the this keyword being complained about:
e(t).bind("click", function(t) {
t.preventDefault();
e(this).parent().fadeOut()
})
Now there’s a lot of problems going on there, but we’ll just focus on one thing at a time.
The function is a part of a click event, where the t
variable represents the event object.
Normally the event object is given a parameter name of evt
, so in the interest of good consistency I’ll rename t
to be evt
// e(t).bind("click", function(t) {
e(t).bind("click", function(evt) {
// t.preventDefault();
evt.preventDefault();
e(this).parent().fadeOut()
})
Don’t rename all of the t variables though, for the one used in e(t)
is a different t from the one that’s used in the function. It’s bad to reuse variable names that are within the same scope of each other, as confusing clashes can occur.
The this keyword refers to the element that was clicked on, which we can easily get from the target
property of the event object.
e(t).bind("click", function(evt) {
evt.preventDefault();
// e(this).parent().fadeOut()
e(evt.target).parent().fadeOut()
})
That is now a lot improved compared with how it began.
Next steps
This has been just a small look at using JSLint to improve the code.
@pavanikaranam - do you want me to give further details about the issues that JSLint has with the code, and what is done to fix those issues?