Hi there,
I have a <select> list with contact names in one of my HTML pages, as well as one button and one text area that will serve as a search function. What I want to know, is how can one go about, by having the user type in a keyword in the textbox, clicking the "search" button. And have the matching <option> highlighted in the list?
I don't know if this will make use of Javascript, altough my guts are telling me that it probably will..
Thanks in advance!
Search function with <select> tag
Page 1 of 13 Replies - 4798 Views - Last Post: 18 March 2011 - 11:21 AM
Replies To: Search function with <select> tag
#2
Re: Search function with <select> tag
Posted 16 March 2011 - 10:07 AM
Working on a solution for you now, I'll repost once it's done.
On a side-note, this would be Javascript, so a moderator may want to move this.
On a side-note, this would be Javascript, so a moderator may want to move this.
This post has been edited by bmcginnis: 16 March 2011 - 05:14 PM
#3
Re: Search function with <select> tag
Posted 17 March 2011 - 12:50 AM
#4
Re: Search function with <select> tag
Posted 18 March 2011 - 11:21 AM
Here you go
I've created 2 different methods for searching through select options.
- highlightOption(color) highlights the searched term the color of argument color.
- highlightOption2(color) highlights the searched term the color of argument color, AND switches the searched term to selected.
The test html I wrote below uses highlightOption(color).
- highlightOption(color) highlights the searched term the color of argument color.
- highlightOption2(color) highlights the searched term the color of argument color, AND switches the searched term to selected.
The test html I wrote below uses highlightOption(color).
<html>
<head>
<title></title>
<script language="Javascript">
function highlightOption(color){
var searchTerm = document.getElementById("searchbox");
var searchBounds = document.getElementById("fruitlist");
for(var i = 0; i < searchBounds.length; i++){
searchBounds[i].style.backgroundColor = "#FFFFFF"; //default color
if(searchBounds[i].value.toLowerCase() == searchTerm.value.toLowerCase()){
searchBounds[i].style.backgroundColor = color;
}
}
}
function highlightOption2(color){
var searchTerm = document.getElementById("searchbox");
var searchBounds = document.getElementById("fruitlist");
for(var i = 0; i < searchBounds.length; i++){
searchBounds[i].style.backgroundColor = "#FFFFFF"; //default color
if(searchBounds[i].value.toLowerCase() == searchTerm.value.toLowerCase()){
searchBounds[i].selected = true;
searchBounds[i].style.backgroundColor = color;
searchBounds.style.backgroundColor = color;
}
}
}
</script>
</head>
<body>
<form>
<select id="fruitlist">
<option>Apple</option>
<option>Blueberry</option>
<option>Cherry</option>
<option>Grape</option>
<option>Lemon</option>
<option>Mango</option>
<option>Orange</option>
<option>Peach</option>
<option>Raspberry</option>
<option>Strawberry</option>
<option>Watermelon</option>
</select>
<input type="text" id="searchbox" />
<input type="button" value="Search" onclick="highlightOption('#FFFF00')" />
</form>
</body>
</html>
This post has been edited by bmcginnis: 18 March 2011 - 05:33 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|