Form object problem...

They have: 40 posts

Joined: Jul 2001

Hi all -

I am attempting to give the user a drop down menu box and have them select one of three options. Then click on the continue image and be sent to a certain page depending on what choice they have made... Here is my code:

with "former" being the name of the form and "word" being the name of the drop down menu...

<script>
function getit() {
if ((document.former.word.value = "test1")){window.location = "index.html"}
else if ((document.former.word.value = "test2")) {window.location = "index1.html"}
else if ((document.former.word.value = "test3")) {window.location = "index2.html"}

}
</script>

I can only get it to send the user to index.html... any suggestions??? Thanks!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Try changing document.former.word.value = "test1" to document.former.word.options[document.former.word.selectedIndex].value == "test1" and so on for the other options.

Don't use single ='s in the if() brackets. Instead, use =='s which mean comparison. Single ='s mean assignment of value.

&lt;script&gt;
function getit() {
if ((document.former.word.options[document.former.word.selectedIndex].value  == "test1")){window.location = "index.html"}
else if ((document.former.word.options[document.former.word.selectedIndex].value  == "test2")) {window.location = "index1.html"}
else if ((document.former.word.options[document.former.word.selectedIndex].value  == "test3")) {window.location = "index2.html"}

}
&lt;/script&gt;
'

They have: 40 posts

Joined: Jul 2001

Thanks Abhishek but any ideas why this only works in IE and not in Netscape? Thanks...

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

What version of NS are you using? This works fine one NS6 for me.

They have: 40 posts

Joined: Jul 2001

4.77

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Sorry, I don't have NS4.7, thus I can't test it.

What do you see in NS, anyway? Does nothing happen, or is there an error message?

I think that it could be a problem with the DOM. NS probably can't find document.former.word.options[document.forms.former.word.selectedIndex].value because the "former" object is in a form. So try document.forms.former.word.options[document.forms.former.word.selectedIndex].value

&lt;script language="JavaScript"&gt;
function getit()
  {
    if (document.forms.former.word.options[document.forms.former.word.selectedIndex].value  == "test1")
      {
        window.location = "index.html";
      }
    else if (document.forms.former.word.options[document.forms.former.word.selectedIndex].value  == "test2")
      {
        window.location = "index1.html";
      }
    else if (document.forms.former.word.options[document.forms.former.word.selectedIndex].value  == "test3")
      {
        window.location = "index2.html";
      }

  }
&lt;/script&gt;
'

I think thats it, but I'm not too sure. Try it anyway.

I also removed the double brackets in the if() statement. if((...)) became if(...).

IE is usually more lenient with double bracketing and shorter DOM's and other stuff. Be extra careful if you're trying to make scripts cross-browser.

They have: 40 posts

Joined: Jul 2001

Abhishek - Maybe I am doing something wrong but this code is not working either (in either IE or NS)...

They have: 40 posts

Joined: Jul 2001

ok - messed with this code a little more... the problem, I believe, has something to do with the form object. I think this because when I change the list/menu form to a text field everything works properly in both IE and NS... what do you think?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Why don't you post the code/URL you've used for the dropdown menu? Also any other code you're using (which might interfere with this). Smiling

Quote:
I think this because when I change the list/menu form to a text field everything works properly in both IE and NS...

- I'm sorry, I don't understand what you've done. Sticking out tongue

The code which I posted previously does work on IE5 for me. The only thing certainly different is our HTML code, and the problem could even lie there.

Rather than have an if() statement for every option you should just have the target page be the option value. E.G:

&lt;script language="JavaScript"&gt;
function getit()
  {
    window.location=document.forms.former.word.options[document.forms.former.word.selectedIndex].value;
  }
&lt;/script&gt;

[...]

<form name="former">
  <select name="word">
    <option value="index.html">First</option>
    <option value="index1.html">Second</option>
    <option value="index2.html">Third</option>
    [...]
  </select>
</form>
<a href="javascript:getit()"><img src="w.xyz"></a>
'

You don't have to execute a comparison for every option. This way you can have as many as you want and change it when you want without touching the JS.

(And this does work on IE5/NS6. It's gotta work on NS4.7)

He has: 39 posts

Joined: Aug 2001

This is what I've done for my sites.

Check out http://www.CyberConneXions.com
(see the drop down at the top right corner)

I've used a drop down to display links to my other sites
(all owned and designed and maintained by yours truly Wink )

I've pasted both the javascript function and the
form (select) code below.

Modify the links to suite your url's. Let me know if you need further help.

I also run a mailing list for webmasters and
do-it-yourself web site owners, where I give away
a lot of such code for free.

Feel free (pun intended) to sign up at
http://www.CyberConneXions.com/totd if you care.

Regards,

Ravi

function loadSite()
{
var mySelect = document.networksites.networksites;

var url = mySelect.options[mySelect.selectedIndex].value;
if(url!="stay")
window.open(url);
}

Other BabyNamesIndia.com Network
Sites
Write2Me.net
CyberConneXions.com
YehHaiIndia.org
BabyNamesIndia.com

Make up to $45 Per Sale
One of the most amazing suite of scripts for the Non-programming Web Site Owner and Webmaster.
http://www.WebmasterInABox.net/

He has: 39 posts

Joined: Aug 2001

And yes, the script above works in all browsers.

Ravi

They have: 40 posts

Joined: Jul 2001

ok all - here is all of the code for the page... I still can't get it to work "at all" in NS 4.77...

Untitled Document

<script>
function getit() {
if ((document.former.word.value == "1")) {window.location = "index.html"}
else if ((document.former.word.value == "2")) {window.location = "index2.html"}
else if ((document.former.word.value == "3")) {window.location = "index3.html"}

}
</script>
Please complete the following search criteria.

*Indicates
a required field.

 

Client
ID Entered:

123456789

*Please
Select a Product:

producta
productb
productc

 

He has: 39 posts

Joined: Aug 2001

You're calling onClick from the wrong place.

It should be:

Remove the other href onclick tag.

Let me know if it works.

- Ravi

He has: 39 posts

Joined: Aug 2001

And forgot to add, your code will not work even if you make that change.

Why?

Because you are not checking for which "select" element is "selected" in your function.

If you don't know what that means, then you won't be able to make your program works.

So, whatever changes you make, it will *NOT* work.

I pasted a fully working script earlier. Why can't you use it? Simply modify my url's to your local url's.

Make up to $45 Per Sale
One of the most amazing suite of scripts for the Non-programming Web Site Owner and Webmaster.
http://www.WebmasterInABox.net/

They have: 40 posts

Joined: Jul 2001

I appreciate you suggestions and the fact that you have posted a working script above. I am following a particular format where I am requiring the user to select an option and then have to click on the continue button. Your example works properly and is a useful script to know but it is nonconforming to the rest of my web where I have similar situations occurring. I have an idea of what you are talking about with the "selecting element" but do not possess the knowledge to remedy the problem... That is where I was hoping you all would come into play!

He has: 39 posts

Joined: Aug 2001

You can easily modify my script to do what you want:

Remove the onChange="java script:loadSite();" in my script and call it from the "OnClick" event ( tag) as I explained in my 2nd example.

- Ravi

Make up to $45 Per Sale
One of the most amazing suite of scripts for the Non-programming Web Site Owner and Webmaster.
http://www.WebmasterInABox.net/

They have: 40 posts

Joined: Jul 2001

oh my goodness, it finally works! woooo hoooo!

one quick question: how do I get the new pages to load in the same window and not in another window?

Thanks again for your help!

He has: 39 posts

Joined: Aug 2001

Just change this:

if(url!="stay")
window.open(url);

to this:
if(url!="stay")
location.href = url;

- Ravi

They have: 40 posts

Joined: Jul 2001

You the man!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.