473,788 Members | 2,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help needed with form selectedIndex property

I cannot get this code to work. Very simple...I have three list
menus. I want to do a check before the form submits to make sure that
the value of the 3 fields is equal to 12.
Here's my code...

<script language="javas cript">
<!--
function addListValues() {
var grandtotal = 0;
var drop1Value,drop 2Value,drop3Val ue;

var drop1Value=docu ment.cakeForm.i ntQuant1.option s[document.cakeFo rm.intQuant1.op tions.selectedI ndex].value;
var drop2Value=docu ment.cakeForm.i ntQuant2.option s[document.cakeFo rm.intQuant2.op tions.selectedI ndex].value;
var drop3Value=docu ment.cakeForm.i ntQuant3.option s[document.cakeFo rm.intQuant3.op tions.selectedI ndex].value;

grandtotal = (drop1Value-0) + (drop2Value-0) + (drop3Value-0);

if (grandtotal < 12) {
alert( "You do not have enough items selected. You need to choose
12 cheesecakes." );
cakeForm.intQua nt1.focus();
document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
return false ;
}

if (grandtotal > 12) {
alert( "You have too many items selected. You need to choose 12
cheesecakes." );
cakeForm.intQua nt1.focus();
document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
return false ;
}

if (grandtotal = 12) {
document.cakeFo rm.submit();
//return false ;
}
}
//-->
</script>
Here is one of my three list menus:

<select name="intQuant1 ">
<option value="0">0</option>
<option value="12">12</option>
<option value="4">6</option>
<option value="4">4</option>
</select>

My submit buttonhas this call... onClick="return addListValues() ;

It does not appear to be working for all three list menus.

Many Thanks
Jul 23 '05 #1
3 5020
Lee
% =joe % said:

I cannot get this code to work. Very simple...I have three list
menus. I want to do a check before the form submits to make sure that
the value of the 3 fields is equal to 12.
Here's my code...

<script language="javas cript">
The <script> tag should have a type attribute:
<script type="text/javascript">
<!--
That line does nothing. Remove it and the matching //-->
function addListValues() {
You could simplify the code, and make it more portable, by passing
arguments into it, instead of making the function know where to
find the values.

var grandtotal = 0;
var drop1Value,drop 2Value,drop3Val ue;
Since you redefine each of these just below, these lines
don't really add anything (if you add a "var" to your
assignment to grandtotal, below).
var
drop1Value=doc ument.cakeForm. intQuant1.optio ns[document.cakeFo rm.intQuant1.op tions.selectedI ndex].value;
var
drop2Value=doc ument.cakeForm. intQuant2.optio ns[document.cakeFo rm.intQuant2.op tions.selectedI ndex].value;
var
drop3Value=doc ument.cakeForm. intQuant3.optio ns[document.cakeFo rm.intQuant3.op tions.selectedI ndex].value;

grandtotal = (drop1Value-0) + (drop2Value-0) + (drop3Value-0);

if (grandtotal < 12) {
alert( "You do not have enough items selected. You need to choose
12 cheesecakes." );
cakeForm.intQua nt1.focus();
That should be document.cakeFo rm... document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
return false ;
}

if (grandtotal > 12) {
alert( "You have too many items selected. You need to choose 12
cheesecakes. " );
cakeForm.intQua nt1.focus();
Again, document.cakeFo rm...
document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
return false ;
}

if (grandtotal = 12) {
The equality comparison operator is "==", not "=".
document.cakeFo rm.submit();
It's better to avoid submit() via Javascript. If you validate
in the onSubmit handler, then returning false will prevent the
form from submitting, and returning anything else will allow
it to submit.
//return false ;
}
}
//-->
Again, these HTML comments are not needed.
There are no longer any browsers in use that need them.
If there were, they wouldn't be able to use your form, anyway.
</script>
Here is one of my three list menus:

<select name="intQuant1 ">
<option value="0">0</option>
<option value="12">12</option>
<option value="4">6</option>
The value should be "6" in that line.

<option value="4">4</option>
</select>

My submit buttonhas this call... onClick="return addListValues() ;


Should be <form .... onsubmit="retur n addListValues(. ..)">

Here's a working example. Don't turn it in as your own homework:

<html>
<head>
<title>Say Cheesecake!</title>
<script type="text/javascript">
function addListValues(q 1,q2,q3) {
var total = +q1.options[q1.selectedInde x].value
+ +q2.options[q2.selectedInde x].value
+ +q3.options[q3.selectedInde x].value;
if(total==12) {
return true;
} else {
alert("You have chosen " + total + " cheesecakes.\n"
+"You must choose exactly 12.");
return false;
}
}
</script>
</head>
<body>
<!-- The following line may wrap when posted -->
<form onsubmit="retur n
addListValues(t his.intQuant1,t his.intQuant2,t his.intQuant3)" >
<select name="intQuant1 ">
<option value="0">0</option>
<option value="12">12</option>
<option value="6">6</option>
<option value="4">4</option>
</select>
<select name="intQuant2 ">
<option value="0">0</option>
<option value="12">12</option>
<option value="6">6</option>
<option value="4">4</option>
</select>
<select name="intQuant3 ">
<option value="0">0</option>
<option value="12">12</option>
<option value="6">6</option>
<option value="4">4</option>
</select>
<input type="submit" value="submit">
</form>
</body>
</html>

Jul 23 '05 #2


Thanks. I appreciate the insight.

If you could point me in one direction to 'sharpen my javascript'
skills, where woul dit be?

Thanks again

joe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
Lee
Joseph Weiss said:

Thanks. I appreciate the insight.

If you could point me in one direction to 'sharpen my javascript'
skills, where woul dit be?


This newsgroup.

Read questions, try to solve the problems by looking
things up on the web or in this newsgroups FAQ or the
resources it points to:

http://www.jibbering.com/faq/

or just by trying things that seem reasonable.
If you can solve the problem, post your answer.
If it's wrong or can be significantly improved,
somebody will let you know.

Either way, read other people's answers.

It really works.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5760
by: Lee Mundie | last post by:
Hi there, Simple problem here but can't seem to fix it! Okay, I have a select list from which people choose avatars... the list is option values ie. <option>Worm</option> <option>Frog</option> etc etc and is in an include file... where the word i.e. worm sources the image ..images/worm.gif - simple so far...
6
1804
by: James Walker | last post by:
Can some one help I get an error of 'checkIndate' is null or not an object can someone please help. I can't work out why Thanks in advance James <form> <td height="24" colspan="7" valign="top"><form name="booknow"><select
7
1913
by: ???????J | last post by:
Javascript may inquire the push down menu value, can I inquire the description? The following example, the variable($answer) can be get the menu1's value. For example, if I select first data, the menu1's value would be 1. Therefore ($answer) would be 1. But If want using javascript get the description(ABC),which javascript command can do it?
1
1310
by: Randi | last post by:
Hi, Thanks David and I got that to work, I guess I still need to get it to work using the arrays and the selectedIndex property. I have to use an alert box that shows the total price like: The cost = the base price + RAM price + Hard Drive Price + Operating System Price The cost = the base price + RAM price + Hard Drive Price + Operating System Price. This is what I have so far, I think i need to use a for loop, because of the array. ...
10
10772
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected = true; Where endTime is a string containing "15".
0
5576
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
7
1704
by: Rotsey | last post by:
Hi, I am loading a tab control on a form. The code loads textboxes and comboboxes and checkboxes, normal data entry form that loads a table row of data. I have a combo on the form above the tab control that filters the form for me.
18
2189
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the SelectedIndex to -1. Do you have any suggestion for getting around this problem?
106
19835
by: bonneylake | last post by:
Hey Everyone, Well i don't know if my question should be in javascript/ajax or coldfusion, i figure this is more of a coldfusion question. But if this is in the wrong section let me know an all gladly delete my question an put it in the correct section. Well what i am trying to do is pretty simple. I have a input field and someone starts typing in a customer number. As they type in the customer number the drop down box is populated. They...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.