Adding select menu default value via JS?

Posted by purpler on Stack Overflow See other posts from Stack Overflow or by purpler
Published on 2010-06-08T10:35:49Z Indexed on 2010/06/08 11:02 UTC
Read the original article Hit count: 257

Filed under:
|
|
|
|

Hi, i'm developing a meta search engine website, Soogle and i've used JS to populate select menu..

Now, after the page is loaded none of engines is loaded by default, user needs to select it on his own or [TAB] to it..

Is there a possibility to preselect one value from the menu via JS after the page loads?

This is the code:

Javascript:

// SEARCH FORM INIT
function addOptions(){
var sel=document.searchForm.whichEngine;for(var i=0;i<arr.length;i++){
sel.options[i]=new Option(arr[i][0],i)}}

function startSearch(){
searchString=document.searchForm.searchText.value;if(searchString!=""){
var searchEngine=document.searchForm.whichEngine.selectedIndex;
var finalSearchString=arr[searchEngine][1]+searchString;location.href=finalSearchString}return false}
function checkKey(e){
var character=(e.which)?e.which:event.keyCode;if(character=='13'){
return startSearch()}}

    // SEARCH ENGINES INIT
    var arr = new Array();
    arr[arr.length] = new Array("Web", "http://www.google.com/search?q=");
    arr[arr.length] = new Array("Images", "http://images.google.com/images?q=");
    arr[arr.length] = new Array("Knoweledge","http://en.wikipedia.org/wiki/Special:Search?search=");
    arr[arr.length] = new Array("Videos","http://www.youtube.com/results?search_query=");
    arr[arr.length] = new Array("Movies", "http://www.imdb.com/find?q=");
    arr[arr.length] = new Array("Torrents", "http://thepiratebay.org/search/");

HTML:

<body onload="addOptions();document.forms.searchForm.searchText.focus()">

<div id="wrapper">
<div id="logo"></div>

<form name="searchForm" method="POST" action="javascript:void(0)">
<input name="searchText" type="text" onkeypress="checkKey(event);"/>
<span id="color"></span>
<select tabindex="1" name="whichEngine" selected="Web"></select>
<br />
<input tabindex="2" type="button" onClick="return startSearch()" value="Search"/>
</form>
</div>

</body>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about forms