1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

This combobox stuff is BS...

Discussion in 'Code Snippets and Tutorials' started by Fexxel, Sep 28, 2009.

  1. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Code (Text):
    1. setTimeout(function () {
    2.     if (location.href.indexOf("app") == -1) var lists = document.getElementsByTagName("listnav1");
    3.     for (var i = 0; i < lists.length; i++) {
    4.         if (lists[i].name == "sortby1") {
    5.             lists[i].options[3].selected = true;
    6.             return true;
    7.         }
    8.     }
    9. },
    10. 0);
    I'm using this site as an example:
    http://appulo.us/appdb/
    Just picked a random site with a combobox.
    Anyways, I'm trying to set a combobox's selected value to the third item.
    So far, it's not working. Doesn't set/change anything. Helps a poor newb?
    PS: I checked google already, found the same code (pretty much) and tried everything I found, none of them worked. So please don't LMGTFY.
     
  2. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    It's currently set up so that if the url does not contain "app" then "var lists = document.getElementsByTagName("listnav1");" and then it goes through the for loop anyways.

    Also listnav1 is an ID not a tagname (ex a, li, img, form, table, tr, td...)
    use document.getElementById("listnav1")

    Since the thing you're looking for has a name, "sortby1", you don't need a for loop to go through all of the lists

    Code (Text):
    1.  
    2. setTimeout(function () {
    3.   if (location.href.indexOf("app") != -1){
    4.     document.getElementsByName("sortby1")[0].options.selectedIndex = 2;
    5.   }
    6. }, 0);
    7.  
    I also prefer to use match instead of indexOf. It's easier for most people to understand. If it matches what's in () then it's true, otherwise it's false. No need to mess with "== -1" or "!= -1" and it's rare that you'll use the index for anything other than matching.
    Code (Text):
    1.  
    2. setTimeout(function () {
    3.   if (location.href.match("app")){
    4.     document.getElementsByName("sortby1")[0].options.selectedIndex = 2;
    5.   }
    6. }, 0);
    7.  
     
  3. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Er... It made no difference.
    Code (Text):
    1. setTimeout(function () {
    2.   if (location.href.match("app")){
    3.     document.getElementsByID("sortby1")[0].options.selectedIndex = 3;
    4.   }
    5. }, 0);
    Doesn't work. I tested it with a different combobox too. :-|
    What's wrong?
    I changed the get elements thing to the following and tested them, none of them worked:
    getElementsByName
    getElementsByID
     
  4. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Probably the fact that you put 3 instead of 2. Remember that indexes start from 0 ;)
    index 0: 1st item
    index 1: 2nd item
    index 2: 3rd item
    ...and so on
     
  5. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    I always test before I post code... Everything I've ever posted for you has worked. So go back to exactly what I had posted and use that.

    If you use getElementById then you don't need [0] afterwards because it's only one element, not an array

    If you use getElementsByName then you'll choose which one you want with [0], [1]... ([0] is the first occurance) since it returns all the elements that fit in an array, even if there's only one.

    The other problem is that "sortby1" is the name not the id.
     
  6. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Alright. I tested it again, I realize what I was doing wrong (thanks to Ricky+Jazz)
    I realized that the second item out of four, and I tried to change it to selected index 3, which would be 5/4.
    Which doesn't work. selected index 2 would mean the 2nd item (the item after the index). So selected index -1 would equal the one before?
    *Still a tad confused* Thanks guys!
     
  7. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    Code (Text):
    1.  
    2. document.getElementsByName("sortby1")[0].options.selectedIndex = 0; // Newest Apps
    3. document.getElementsByName("sortby1")[0].options.selectedIndex = 1; // Newest Apps & Updates
    4. document.getElementsByName("sortby1")[0].options.selectedIndex = 2; // App Name
    5. document.getElementsByName("sortby1")[0].options.selectedIndex = 3; // Search Relevance
    6.  
    or if you know the value try any of the following:
    Code (Text):
    1.  
    2. document.getElementsByName("sortby1")[0].value = "newapps";   // Newest Apps
    3. document.getElementsByName("sortby1")[0].value = "newvers";   // Newest Apps & Updates
    4. document.getElementsByName("sortby1")[0].value = "appname";   // App Name
    5. document.getElementsByName("sortby1")[0].value = "relevance"; // Search Relevance
    6.