Forum Moderators: open

Message Too Old, No Replies

drop down menu manipulation on "back button"

         

Bostonrose

6:11 pm on Jun 21, 2002 (gmt 0)



I have a form that is a series of drop down menus where only one select is allowed before hitting submit. Once on the next page, if the user hits the back button, the same select name shows in the series of drop down menus, but I really want to re-initialize all the drop down menus back to "Select keyword" which is the beginning select item on a new page. How do I get them to all reset on the back button. Secondly I also want to be able to edit that only one of them is selected at a time. I can do that with ASP on the next page and then redirect, but would much rather use javascript put the error up right on that page rather than go to the next page and redirect. I'm pretty new with javascript, so thanks for any help you can give.

toadhall

7:03 pm on Jun 21, 2002 (gmt 0)

10+ Year Member



Hi Bostonrose, welcome to the board.

I can help you with the first part of your problem, namely resetting to "Select keyword", but some of the more experienced javascripters will have to help with the second. They'll be around I'm sure.

Add a name to your form as in: <form name=bobtheform ...>
And add this to your body tag: onLoad="document.bobtheform.reset();"

jatar_k

7:15 pm on Jun 21, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WmW Bostonrose

If I understand correctly you have multiple drop downs and want them only to pick a value in one and then hit a "go" button or something. If this is the case I would change to behaviour of the drop downs so that they "go" as soon as a value is selected allowing them to only make a single selection.

<select name="elementname" onChange="switchurl();">

or something like that

rewboss

7:45 pm on Jun 22, 2002 (gmt 0)

10+ Year Member



I'd recommend redesigning the form -- it sounds very confusing, unless you've designed the user interface really, really well.

Here's an attempt at an event handler which, when you hit the submit button, will check every drop-down menu and sumbit the form if, and only if, exactly one item has been selected. The <form> tag should be written like this:

<form action="next.asp" onsubmit="return check(this);">

The script looks like this:

function check(f){
var num_selected=0;
for(var element in f.elements){
if(f.elements[element].type!='select-one') continue;
if(f.elements[element].selectedIndex>0) num_selected++;
}
if(num_selected==1) return true;
alert('You must select one item only');
return false;
}

It works by looping through all the elements of the form, skipping those that are not drop-down menus. For each drop-down menu, it checks that an item has been selected, and that it's not the first item (which would be your "Please select" line). If you don't have "Please select..." as your first line, you will need to change the condition to...

if(f.elements[element].selectedIndex>=0)...

(A value of 0 means "first item selected", a value of -1 means "no item selected"). Every time it finds a menu with an item selected, it increments the value of num_selected.

Having looped through the elements, it then tests to see how many items have been selected. If the answer is 1, the function ends and returns true, which causes the form to be submitted. Otherwise it pops up an error message and returns false, which cancels the submission process.

Personally, I think jatar_k's solution is the better one, but you can study this example anyway. (No guarantees, though; I haven't tried it out.)

Don't ditch the server-side test, though. Any client-side JavaScript solution relies on JavaScript being enabled, so you'll still need the server-side script as a fail-safe backup.

papabaer

11:04 pm on Jun 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Bostonrose - Welcome to WebmasterWorld!

It sounds as if you are attempting the type of form that allows a user to make multiple selections similar the the job/category1/category2/region1/region2/keywords type form that jobsites such as Monster.com employ. I've never needed to create a form as sophisticated as this so I cannot speak from experience on this issue. Toadhall, jatar_k and rewboss have all offered some good options and I expect some other members may have other suggestions that may help you target a solution.

Once again.. welcome to the forums!

Bostonrose

11:41 am on Jun 23, 2002 (gmt 0)



Thanks all for the help. I tried all the suggestions and all of them worked, so I have plenty of solutions. Bostonrose