I want to make it so that when you select (click) on an li element it toggles a class.I've tried:
$("li").click(function() {
$(this).toggleClass("selected");
});
but nothing happens when I click the list item.
EDIT:
HTML:
<input type = "text" placeholder = "New category" id = "category">
<button type = "button" onclick ="addCategory()">Add</button><br>
<ul id = "list">
<li>School</li>
<li>Home</li>
<li>Fun</li>
</ul>
Rest of JS/jQuery:
function addCategory() {
var category = $("#category").val();
var list = $("#list");
var li = $("<li>");
list.append(li);
li.append(category);
}
$("li").click(function() {
$(this).toggleClass("selected");
});