Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 24th, 2004, 07:14 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help needed with variables and functions

Hello,

I've got myself into a bit of a pickle with this and help would be greatly appreciated. I'm trying to populate a drop-down list with values dependant upon a selection made in another drop down list. This works. However, the end result is that the user clicks a go button to be taken to the location they selected in the second drop down list. The problem I have is that the address it goes to looks like this: P:\directory\subdirectory\undefined

The script looks like this:

<html>

<head>
<title>New Page 1</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>

<body>

<form name="form1">
  <p><select name="sel" size="1" onchange="doit()">
    <option value="0"></option>
    <option value="1"></option>
  </select> <input type="text" size="10" name="text1"> </p>
  <p><select name="sel2" onchange="doit2()" size="1">
  </select> </p>
  <p><input type="Button" onclick="GoThere()" value="Warp"> </p>
</form>
<script language="Javascript">
var myNewOption = new Option("A choice", "0")
var Selected=document.form1.elements['sel']
var Selected2=document.form1.elements['sel2']

document.form1.sel.options[0] = myNewOption;

function doit(){
var myNewOption2 = new Option("Two", "address.htm")
var myNewOption3 = new Option("Three", "anotheraddress.htm")
if (Selected.selectedIndex==0);
{
document.form1.sel2.options[0] = myNewOption2
}

if (Selected.selectedIndex ==1);
{
document.form1.sel2.options[1] = myNewOption3
}
}


function doit2(){
var myNewOption4 = new Option ("Four","3")
if (Selected2.selectedIndex ==1);
{
document.form1.sel.options[1] = myNewOption4
document.form1.sel.options[0] = myNewOption
}
}

function GoThere(){
location.href = document.form1.sel2.selectedIndex.value
}

</script>

</body>
</html>


Help!

Cheers
Interrupt

__________________
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
 
Old May 24th, 2004, 08:22 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey Interrupt,

The problem is with how you are accessing the value of the selected option in "sel2". Give this a go...

function GoThere(){
    var ddl = document.form1.sel2;
    if(ddl.selectedIndex > -1){
        var value = ddl.options[ddl.selectedIndex].value;
        location.href = value;
    }else{
        alert("please select an option");
    }
}

HTH,

Chris

 
Old May 24th, 2004, 08:47 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris,

That was just the job, thank you very much, I was ready to throw my box out the window! Nice one.

interrupt






Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning Automatic Variables from Functions jabney BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 2 November 29th, 2008 09:56 PM
Can variables be pulled out of functions Apocolypse2005 Javascript How-To 3 July 21st, 2006 05:16 PM
Help with functions and variables DeadlyDesigns.NET Beginning PHP 6 January 26th, 2005 12:28 AM
Functions and variables collie VB.NET 2002/2003 Basics 3 February 5th, 2004 02:45 AM
Global variables and functions madhukp ASP.NET 1.x and 2.0 Application Design 3 October 9th, 2003 09:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.