From what I understand you are asking to update the search box value every time the user clicks on a result.
If so here is a simple demonstration of how you can get that done:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Selecting Search Results</title>
<!-- NOTE: we need jQuery so lets include it -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<!-- NOTE: this is your search panel -->
<div class="search-panel">
<input type="text" class="search-box"/>
</div>
<!-- NOTE: this is your results container -->
<div class="results-container">
<div class="result">Lorem Ipsum 1</div>
<div class="result">Lorem Ipsum 2</div>
<div class="result">Lorem Ipsum 3</div>
<div class="result">Lorem Ipsum 4</div>
<div class="result">Lorem Ipsum 5</div>
<div class="result">Lorem Ipsum 6</div>
<div class="result">Lorem Ipsum 7</div>
<div class="result">Lorem Ipsum 8</div>
</div>
<!-- NOTE: this simple script updates the search box value -->
<script type="text/javascript">
$(document).ready(function() {
$(".results-container > .result").on("click", function(event) {
$(".search-panel > .search-box").val($(this).text());
});
});
</script>
</body>
</html>
Here is a break down of the jQuery that updates the search box value:
// wait until we can safely manipulate the page.
$(document).ready(function() {
// listen for click events on the results
$(".results-container > .result").on("click", function(event) {
// update the search box value
$(".search-panel > .search-box").val($(this).text());
});
});
If you would like to use Vanilla JavaScript then use this instead:
// wait until we can safely manipulate the page.
document.addEventListener("DOMContentLoaded", function() {
// get the results
var results = document.querySelectorAll(".results-container > .result");
for(var i = 0; i < results.length; i++) {
// listen for click events on the results
results[i].addEventListener("click", updateValue);
}
// this function handles the "click" event
function updateValue() {
// update the search box value
document.querySelector(".search-panel > .search-box").value = (this.textContent || this.innerText);
}
}, false);
Also I have not put this function in the for loop because it is bad practice to put functions in loops, for more info read this page.
Good luck and all the best.