Commit b2186fa9 authored by Porter Libby's avatar Porter Libby
Browse files

create simple autofill search for careers

parent a53d605a
// Render menu for the career object
// Porter Libby 2019
console.log(careers); // show json structure from 'data/careers-data.js'
var container_name = 'Career';
var container = document.getElementById(container_name);
//create search obj
var search_container = document.createElement("div");
search_container.id = 'career-search-container';
var search = document.createElement("INPUT");
search.id = 'myInput'
search.setAttribute("type", "text");
search.setAttribute("value", "");
prompt = document.createElement('span');
prompt.innerHTML = 'Search for Careers:';
search_container.appendChild(prompt);
search_container.appendChild(search);
container.appendChild(search_container);
names_for_search = [];
for(x=0;x<careers.length;x++){
console.log(careers[x].title);
names_for_search.push(careers[x].title);
}
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
autocomplete(document.getElementById("myInput"), names_for_search);
......@@ -89,4 +89,33 @@ p {
width: 60vw;
height: 60vw;
}
}
/* STUFF FOR CAREERS (PORTER) */
input{
width: 75%;
align-self: bottom;
}
#career-search-container{
width: 100%;
height: 100%;
padding-top: 50%;
text-align: center;
}
#career-search-container span{
font-size: 12px;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
background-color: #e9e9e9;
}
.autocomplete-active {
background-color: DodgerBlue !important;
color: #ffffff;
}
\ No newline at end of file
var careers = [
{
"title": "Bioinformatics"
},
{
"title": "Biologist"
},
{
"title": "Web Developer"
},
{
"title": "Software Engineer"
},
{
"title": "Highschool Teacher"
},
{
"title": "Middleschool Teacher"
}
]
\ No newline at end of file
......@@ -11,5 +11,7 @@
<div id='flow-chart-container'>
</div>
<script src="main.js"></script>
<script src="data/careers-data.js"></script>
<script src="careers.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -85,6 +85,7 @@ function main(){
// create div of specific shape
var elem = document.createElement("div");
elem.className = "flow-chart-block flow-chart-" + items[i].type;
elem.id = items[i].name;
// create text to go in div
var text = document.createElement("p");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment