[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript Form Input Undefined

Posted on 2011-10-31
7
Medium Priority
?
142 Views
Last Modified: 2013-11-20
I have a select box on a page (that I will give the alias of page.asp) that calls a function:
<select name="make" onchange="return submitValues();">

Here is the javascript function:
function submitValues(){
		var Make = document.parts.make.value;
		window.location = "page.asp?make="+Make;
		return false;
	}

Open in new window


When I choose a value from the select box, the function sends the user to page.asp?make=ford.  Now, the select box is on page.asp so this function simply sends the user to page.asp again but with some URL variables this time.  I then take the URL variables and use them to determine which option of the select box make should be "selected."

The problem occurs when I select a new option from the select box.  The function executes and sends me to page.asp?make=undefined.  For some reason, the function correctly grabs the option value of the make select box the first time, but on the second time it cannot seem to pull the right value; it simply gives the value as "undefined."
0
Comment
Question by:nickgross19
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37058871
You may have better luck iterating through the options to see which one is selected.

function submitValues(){
                var MakeObj = document.parts.make, Make;
                for (var i=0; i<MakeObj.options.length; i++;) {
                                if (MakeObj.options[i].selected) { Make = MakeObj.options[i].value; }
                        }
                window.location = "page.asp?make="+Make;
                return false;
        }

Open in new window

0
 

Author Comment

by:nickgross19
ID: 37058926
Hmm, interesting. However, there are multiple select boxes on this page and if the user chooses to change the make select box again, then I need the value to be the new value they selected and NOT the one that the html option code has "selected" written in it.  Does your solution work even if the user chooses a new value after they already submitted one?
0
 

Author Comment

by:nickgross19
ID: 37059348
I tried using the code but there must have been an error as it didn't even send the user to the new location:

var Make = "";
	    var MO = document.partInfo.make;
		for (var i=0; i<MO.options.length; i++;) {
			if (MO.options[i].selected) { Make = MO.options[i].value; }
		}
		window.location = "Page.asp?make="+Make;
		return false;

Open in new window

0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37059500
Sorry, take out the semi-color after 1++ in the for loop parameters

 
var Make = "";
            var MO = document.partInfo.make;
                for (var i=0; i<MO.options.length; i++) {
                        if (MO.options[i].selected) { Make = MO.options[i].value; }
                }
                window.location = "Page.asp?make="+Make;
                return false;

Open in new window

0
 

Accepted Solution

by:
nickgross19 earned 0 total points
ID: 37059647
I found that I was missing a closing </form> tag and so I put that in and I was able to use a different javascript code to do what I needed done.  I don't know if the closing form tag caused this problem with this javascript, but I would suspect that the missing tag was the reason why my code didn't work.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question