www.webdeveloper.com
Results 1 to 3 of 3

Thread: Adding select menu default value via JS?

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Question Adding select menu default value via JS?

    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:
    Code:
    // 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:
    Code:
    <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>

  2. #2
    Join Date
    Feb 2010
    Location
    Amsterdam
    Posts
    174
    you can use .selectedIndex = 1;

    Code:
    // make reference to select
    var sel = document.getElementsByTagName('select')[0]; // first select (gives back array)
    // or 
    var sel = document.getElementById('selectId');
    sel.selectedIndex = 2; // voila

    if you want to do this when the page loads do it through the window.onload event or add it before the </body>
    Code:
    <script type="text/javascript">
    var sel = document.getElementsByTagName('select')[0]; // first select (gives back array)
    sel.selectedIndex = 2; // voila
    </script>
    </body>

  3. #3
    Join Date
    Jun 2010
    Posts
    2
    hmm, seems it doesnt work..
    Here is the URL, maybe you could check it better that way..

    http://soogle.net/

    Thanks for helping!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles