Adding select menu default value via JS?

Discussion in 'HTML & Website Design' started by purpler, Jun 8, 2010.

  1. #1
    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/");
    Code (markup):
    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>
    Code (markup):
     
    purpler, Jun 8, 2010 IP