I have a searchbar that autofills the words, i want to make the autofilled words links. so i have a page called page2.html and i want it so that if i type locations it will autofill the word locations en when i click on the autofilled word it will direct me to my Page2.html.
If i go to my searchbar and type Locations i get nothing, if i type "<" i get the code. it doesnt make it a link.
This is my html/javascript, there is a code that targets the arry down at the bottom:
<form autocomplete="off" action="/action_page.php">
<div class="SearchBar">
<input id="myInput" type="text" name="Search2" placeholder="Search...">
</div>
</form>
<script>
var SW = ["<a href='Page2.html'>Locations</a>"];
function SearchBar(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 + "SearchBar-list");
a.setAttribute("class", "SearchBar-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 + "SearchBar-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) {
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("SearchBar-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("SearchBar-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("SearchBar-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);
});
}
</script>
<script>
SearchBar(document.getElementById("myInput"), SW);
</script>
You can create the suggestion items using document.createElement
.
Like this:
var a = document.createElement("a");
a.href = "page2.html";
a.innerHTML = arr[valIndex];
container.appendChild(a) // add it to the dom; container == the element the suggestion should be put in
Where arr[valIndex]
is the suggested item from your array. Let me know in the comments if you need the whole code;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.