Build an id-generator for me!
HTML
<h1>
Learn to use Closures in JS
</h1>
<button id="hansi_button">Hansi</button>
<button id="susi_button">Susi</button>
<p>These buttons schould create spans with uniq ids!</p>
CSS
span {
border-radius: 3px;
padding: 3px;
margin: 1px;
}
.hansi {
background-color: #F19020;
}
.susi {
background-color: green;
}
JavaScript
function idGenerator(name) {
// write the missing code here!
var idname = name
return function(){
return ++idname;
}
}
const nextHansiId = idGenerator("hansi");
const nextSusiId = idGenerator("susi");
$('#hansi_button').on('click', function(ev) {
let id = 'hansi_7'; nextHansiId(); // enable this!!!!
$('body').append(`<span id="${id}" class="hansi">span ${id}</span>`);
})
$('#susi_button').on('click', function(ev) {
let id = 'susi_8'; nextSusiId(); // enable this!!!!
$('body').append(`<span id="${id}" class="susi">span ${id}</span>`);
})