Skip to content

Instantly share code, notes, and snippets.

@blake41
Last active August 29, 2015 14:21
Show Gist options
  • Save blake41/8ff37ea496d3064b39d9 to your computer and use it in GitHub Desktop.
Save blake41/8ff37ea496d3064b39d9 to your computer and use it in GitHub Desktop.
.hidden {
visibility: hidden;
}
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<ul class="courses"></ul>
</body>
</html>
function liTemplate(id, input, details){
return '<li class="course" data-details="' +
details +
'">' +
input +
'<span data-id="' + id + '"> &times;</span></li>';
}
function detailsTemplate(details){
return'<div class="details">' +
details +
'</div>';
}
function appendToUl(ul, item) {
ul.append(liTemplate(item.id, item.name, item.details));
}
$(document).ready(function() {
courses = [{"name":"ruby","details":"the language of ballers","id":1},
{"name":"python","details":"a snaky language","id":2},
{"name":"javascript","details":"JUNK!","id":3},
{"name":"haskell","details":"check out my neckbeard","id":4}]
var $courses = $('.courses');
courses.forEach(function(item) {
appendToUl($courses, item);
})
$('.courses').on('click', '.course', function(event) {
if ($(this).find('.details').length === 0) {
var $li = $(this);
var details = $li.data('details');
$li.append(detailsTemplate(details));
} else {
$(this).find('.details').remove();
}
})
});
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript" src="jqueries.js"></script>
<link rel="stylesheet" type="text/css" href="myStylez.css">
</head>
<body>
<ul class="courses">
<li>Ruby</li>
<li>Python</li>
<li>Javascript</li>
<li>Haskell</li>
</ul>
<select id="selector">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
var subselect = {
"Ballers" : ["Steph", "Geraldina", "Jordan"],
"Programmers" : ["Geraldina", "Steph", "Andrew"],
"Dancers" : ["Lezou", "Mykle", "David"],
"Rappers" : ["Mykle","Earl"]
}
$(function() {
$("#selector").change(replaceSelect);
})
function replaceSelect() {
$("body").append(buildSelectBox());
}
function buildSelectBox() {
if ($("#subselect").length > 0) {
$("#subselect").remove();
return addSelectBox();
} else {
return addSelectBox();
}
}
function addSelectBox() {
return '<select id="subselect">'
+ buildOptions() +
'</select>'
}
function buildOptions() {
var optionString = ""
var selected = $("#selector option:selected").text();
subselect[selected].forEach(function(element) {
optionString += buildOption(element);
})
return optionString;
}
function buildOption(element) {
return '<option value="' + element + '">' + element + '</option>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment