Jump to content

Problem with...well everything


spoonraker

Recommended Posts

Ok, this is a little complicated so bear with me and try and understand my problem.I have a website with 3 dropdown menus. The first one is a filter, things like name, rank, etc. The second one is an operator for that filter, either =, >, or <. The third one is a list of options for a particular filter, for example if you select rank as the filter you get options like Affiliate, executive, etc.Now... in case you didn't figure this out on your own, the first dropdown is static, and the other two are dynamic based on what you chose in the first. I have absolutely no trouble generating these menus, that has been in place for a while.What my problem is, is that when the form is submitted, I need the refreshed page to pre-populate those forums with the saved data.The data for the three drop downs (Dropdown IDs in order : "filterby", filterbysize", "filterbyvalue") is stored in Java/JSP.So...I created a javascript function which was supposed to run on page load using the body onLoad tag.Well the first problem is that my javascript function doesn't work. What I planned on doing was doing a reverse looking up the select option indexes by value, and then passing that index to a function which sets that select to that option. Here is my code, now I know that it's a little bit different than I described, this is because there is another set of filters, the first one has a text field as the last option, and the second one is the one I described with three select menus.

function getIndexOfValue(selectBox, lookupValue){					var index = 1;					for (i = 1; i < selectBox.options.length; i++) {	 				   if (selectBox.options[i].value == lookupValue) {							index = i;						}					}					return index;				}								function changeSelection_(selectBox, index){					selectBox.options.selectedIndex = index;				}								function populateFilters(){									var filterbysize = document.getElementById("filterbysize");					var filterbysizevalue = "<%=genealogy.getFilterbysize()%>";										var filterbysizetwo = document.getElementById("filterbysizetwo");					var filterbysizetwovalue = "<%=genealogy.getFilterbysizetwo()%>";															var filterbyvaluetwo = document.getElementById("filterbyvaluetwo");					var filterbyvaluetwovalue = "<%=genealogy.getFilterbyvaluetwo()%>";										changeSelection(filterbysize, getIndexOfValue(filterbysize, filterbysizevalue));					changeSelection(filterbysizetwo, getIndexOfValue(filterbysizetwo, filterbysizetwovalue));					changeSelection(filterbyvaluetwo, getIndexOfValue(filterbyvaluetwo, filterbyvaluetwovalue));								}

The function be to run at page load is populateFilters(). Using alerts I have determined that the code stops executing once it hits the last three changeSelection(... commands. If I comment those out and place an alert after all the variable declarations it works fine. Also, the jsp <%= %> statements aren't the problem, they work fine, i can print out the values using a JS alert.So that's my first problem, my reverse lookup and option changing scripts dont work. Any ideas?No my second problem is pretty simple. The body onLoad tag doesn't work. If I do body onLoad="populateFilters()", nothing happens. Even if I do body onLoad="alert('hello')" I never see the alert. But when I take my populateFilters() function and execute it in an onClick event, it executes fine (except the errors talked about above obviously)Any help is appreciated

Link to comment
Share on other sites

I'm a little confused by the first part you wrote, but it doesn't matter. First, it would be helpful to know what data the JSP page is outputting. If you could view the source of the output and paste that it would be helpful. Either the changeSelection or getIndexOfValue function is causing an error. You can either check for error messages in the browser (which you should do anyway), or add alerts to both of those functions to say what parameters they were called with. If changeSelection never gets called then the error is in getIndexOfValue. If changeSelection does get called then the error is there.For the onload, something else is probably overwriting it. You can either find what is doing that and change it to add a new onload event instead of overwriting the existing one, or add script tags at the end of the body (or even just after the select elements get created) to run the function explicitly.

Link to comment
Share on other sites

I'll make this simple...this web page I'm working on is incredibly huge and complicated (I'm well over 2,000 lines of Java) and trying to post the "big picture" would just confuse the ###### out of everybody...so here is exactly what I need.I have a select box like so...<select id="filter"><option value="blah">blah</option><option value="bleh">bleh</option><option value="bluh">bluh</option></select>The ONLY data I have to work with is the "value" of the option selected, "blah", "bleh", or "bluh".So how would I use javascript to select the correct option?The method I'm currently trying uses a reverse lookup. It loops through the options one at a time, and when the value matches the value I pass in, it returns the index of that option. Then I use document.getElementById("filter").options.selectedIndex = X to set the selectbox to the correct option.The code for that is posted in the OP, but it's not working.

Link to comment
Share on other sites

I have narrowed my problem down to the getIndexOfValue() method.If I just call changeSelection(whatever, 2), it works just fine.Thanks for any help.*update* I noticed in this line

for (i = 1; i < selectBox.options.length; i++) {

that I forgot the "var" in the for loop, I changed it to

for (var i = 1; i < selectBox.options.length; i++) {

It still doesn't work.Oh and I get zero javascript errors.

Link to comment
Share on other sites

Technically, selectBox.options returns a "collection," not a true array, so trying to access selectBox.options.length might lead to an error. I seem to remember this. Are you working in IE? Anyway, selectBox.length IS a valid DOM property and means exactly what it should. It's what I use, anyway, and I doubt I would if the more intuitive version (yours) had worked.Give it a try.I assume you're starting your iteration from 1 because 0 holds a label or something?

Link to comment
Share on other sites

Ok well it turns out my script works just fine, I'm just getting strange errors with select boxes not having properties and stuff.I'm using this DynamicOptionList javascript function to handle my dynamic lists. http://www.mattkruse.com/javascript/dynamicoptionlist/When I reference a select box that I manually create and add options to, both the getIndexOfValue() and changeSelect() functions work fine, but when I pass it a select box handled by that script, it always tells me that "selectBox has no properties" even though I can see all the options on my screen.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...