Code (Text): setTimeout(function () { if (location.href.indexOf("app") == -1) var lists = document.getElementsByTagName("listnav1"); for (var i = 0; i < lists.length; i++) { if (lists[i].name == "sortby1") { lists[i].options[3].selected = true; return true; } } }, 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.
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): setTimeout(function () { if (location.href.indexOf("app") != -1){ document.getElementsByName("sortby1")[0].options.selectedIndex = 2; } }, 0); 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): setTimeout(function () { if (location.href.match("app")){ document.getElementsByName("sortby1")[0].options.selectedIndex = 2; } }, 0);
Er... It made no difference. Code (Text): setTimeout(function () { if (location.href.match("app")){ document.getElementsByID("sortby1")[0].options.selectedIndex = 3; } }, 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
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
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.
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!
Code (Text): document.getElementsByName("sortby1")[0].options.selectedIndex = 0; // Newest Apps document.getElementsByName("sortby1")[0].options.selectedIndex = 1; // Newest Apps & Updates document.getElementsByName("sortby1")[0].options.selectedIndex = 2; // App Name document.getElementsByName("sortby1")[0].options.selectedIndex = 3; // Search Relevance or if you know the value try any of the following: Code (Text): document.getElementsByName("sortby1")[0].value = "newapps"; // Newest Apps document.getElementsByName("sortby1")[0].value = "newvers"; // Newest Apps & Updates document.getElementsByName("sortby1")[0].value = "appname"; // App Name document.getElementsByName("sortby1")[0].value = "relevance"; // Search Relevance