473,799 Members | 3,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript help

I'm not sure why my checkbox code is not working as per intended. It
always keeps saying "Please select the department" even though I check
the department... appreciate any help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TI TLE>Welcome to the Scripting Store</TITLE>
<META http-equiv=Content-Type content="text/html;
charset=windows-1252">
<SCRIPT>

// Banner Rotator logic

//Load banner elements into array
var banners = new
Array("banner1. jpg","banner2.j pg","banner3.jp g","banner4.jpg ")

//Set banner counter to 0
var bnrCntr = 0

//Function that actually rotates the banner
function bancycle() {
bnrCntr = bnrCntr + 1
if (bnrCntr == 4) {
bnrCntr = 0
}
document.Banner .src = banners[bnrCntr]
setTimeout("ban cycle()",3000)
}

//End Banner Rotator Logic

</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR> </HEAD>
<BODY onload=bancycle ()>
<TABLE borderColor=blu e width=700 border=1>
<TBODY>
<TR>
<TD width="30%"><IM G name=Banner></TD>
<TD align=middle>
<H1>Welcome to the Scripting
Store</H1></TD></TR></TBODY></TABLE><BR>
<SCRIPT language=javasc ript>

document.writel n("Today is: ");
var nDate = new Date();
document.writel n(nDate);

var strName = new String();
strName = window.prompt(" What is your Name?", "FirstNameOnly" );
//document.write( "Welcome " + strName + "<br>");

document.write( "<h2>Hello " + strName + "</h2>");
var varStatus;
status = "Hello " + strName + " - Welcome to the Scripting Store";
//Year(Now());
</SCRIPT>

<P>Please select one of our departments listed below</P><BR><INPUT
onclick=locateD epartment() type=button value="Select Department"
name=btnSelect>
<BR>
<SCRIPT>

var varDept = new Array(3);
varDept[0] = "Books";
varDept[1] = "Computers" ;
varDept[2] = "Caffeine free products";
var numMax = varDept.length;
var strDept;

for (var i=0; i <numMax; ++i)
{
document.write( "<p><input type='checkbox' value=" + [i] + "name=" +
varDept[i] + ">" + varDept[i] + "</p>");
}
</SCRIPT>

<SCRIPT>

function locateDepartmen t()
{
var varselect = true;
var varMax = 3;
for (var x=0; x < varMax; ++x)
{
if (varDept[x].checked == true)
{
if ([x] == 0)
{
location.href=' book.htm';
}
else if ([x] == 1)
{
location.href=' Caffiene.htm';
}
else if ([x] == 2)
{
location.href=' Computers.htm';
}
}
else
{
varselect = false;
}
}

if (varselect == false)
{
window.alert("P lease Select a Department");
return false;
}

}

</SCRIPT>
</BODY></HTML>
Jul 23 '05 #1
6 1631
ak******@guidem ail.com (Andy) wrote in message news:<ac******* *************** ****@posting.go ogle.com>...
I'm not sure why my checkbox code is not working as per intended. It
always keeps saying "Please select the department" even though I check
the department... appreciate any help.
I think it is a problem with your loop in the locateDepartmen t
function.

I commented out the location.href links and replaced them with alert
boxes. This did not work.

I commented out the end else loop. This stopped the alert box but
nothing happens with when Select Department button is press when a
checkbox is checked. It seems the loop automatically brings you to
this else statement.

I moved all functions within the <HEAD> tags, did not help.

Not sure either. Attempted for one hour without luck.

Good luck,

A Javascript newbie

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TI TLE>Welcome to the Scripting Store</TITLE>
<META http-equiv=Content-Type content="text/html;
charset=windows-1252">
<SCRIPT>

// Banner Rotator logic

//Load banner elements into array
var banners = new
Array("banner1. jpg","banner2.j pg","banner3.jp g","banner4.jpg ")

//Set banner counter to 0
var bnrCntr = 0

//Function that actually rotates the banner
function bancycle() {
bnrCntr = bnrCntr + 1
if (bnrCntr == 4) {
bnrCntr = 0
}
document.Banner .src = banners[bnrCntr]
setTimeout("ban cycle()",3000)
}

//End Banner Rotator Logic

</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR> </HEAD>
<BODY onload=bancycle ()>
<TABLE borderColor=blu e width=700 border=1>
<TBODY>
<TR>
<TD width="30%"><IM G name=Banner></TD>
<TD align=middle>
<H1>Welcome to the Scripting
Store</H1></TD></TR></TBODY></TABLE><BR>
<SCRIPT language=javasc ript>

document.writel n("Today is: ");
var nDate = new Date();
document.writel n(nDate);

var strName = new String();
strName = window.prompt(" What is your Name?", "FirstNameOnly" );
//document.write( "Welcome " + strName + "<br>");

document.write( "<h2>Hello " + strName + "</h2>");
var varStatus;
status = "Hello " + strName + " - Welcome to the Scripting Store";
//Year(Now());
</SCRIPT>

<P>Please select one of our departments listed below</P><BR><INPUT
onclick=locateD epartment() type=button value="Select Department"
name=btnSelect>
<BR>
<SCRIPT>

var varDept = new Array(3);
varDept[0] = "Books";
varDept[1] = "Computers" ;
varDept[2] = "Caffeine free products";
var numMax = varDept.length;
var strDept;

for (var i=0; i <numMax; ++i)
{
document.write( "<p><input type='checkbox' value=" + [i] + "name=" +
varDept[i] + ">" + varDept[i] + "</p>");
}
</SCRIPT>

<SCRIPT>

function locateDepartmen t()
{
var varselect = true;
var varMax = 3;
for (var x=0; x < varMax; ++x)
{
if (varDept[x].checked == true)
{
if ([x] == 0)
{
location.href=' book.htm';
}
else if ([x] == 1)
{
location.href=' Caffiene.htm';
}
else if ([x] == 2)
{
location.href=' Computers.htm';
}
}
else
{
varselect = false;
}
}

if (varselect == false)
{
window.alert("P lease Select a Department");
return false;
}

}

</SCRIPT>
</BODY></HTML>

Jul 23 '05 #2
Lee
Andy said:

I'm not sure why my checkbox code is not working as per intended. It
always keeps saying "Please select the department" even though I check
the department... appreciate any help. <SCRIPT>
The <script> tag should have a type attribute:

<script type="text/javascript">
var varDept = new Array(3);
varDept[0] = "Books";
varDept[1] = "Computers" ;
varDept[2] = "Caffeine free products";
var numMax = varDept.length;
var strDept;

for (var i=0; i <numMax; ++i)
{
document.write ("<p><input type='checkbox' value=" + [i] + "name=" +
varDept[i] + ">" + varDept[i] + "</p>");
}
</SCRIPT>

<SCRIPT>

function locateDepartmen t()
{
var varselect = true;
var varMax = 3;
for (var x=0; x < varMax; ++x)
{
if (varDept[x].checked == true)
You've defined varDept as an array of strings.
None of the elements of this array have an attribute
named "checked".

{
if ([x] == 0)


What are those square brackets supposed to mean?
That's a syntax error.

Jul 23 '05 #3
Lee wrote:
<snip>
if ([x] == 0)


What are those square brackets supposed to mean?
That's a syntax error.


It is not a syntax error, it is an Array literal: Construct an new Array
with its first element set to the current value of x, compare the result
(the Array object) with the numeric value of zero and when it doesn't
equal zero do not execute the statement following - if -.

Completely valid javascript, just massively unlikely to be anything even
close to what is intended.

Richard.
Jul 23 '05 #4
On 6 Oct 2004 12:21:00 -0700, Andy <ak******@guide mail.com> wrote:

[snip]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Some browsers don't treat that DOCTYPE properly. Include the URL. See
<URL:http://www.w3.org/TR/html4/struct/global.html#h-7.2>.
<HTML><HEAD><TI TLE>Welcome to the Scripting Store</TITLE>
Hmm. That title worries me.
<META http-equiv=Content-Type
content="text/html; charset=windows-1252">
Don't use Microsoft character sets. Use standard ones. ISO-8859-1 (Latin
1) will be fine for English websites.

<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<SCRIPT>
As Lee said: the type attribute is required for SCRIPT elements.

[snip]
<META content="MSHTML 6.00.2800.1106" name=GENERATOR> </HEAD>
Using any Web authoring software produced by Microsoft (Notepad being the
only exception) is generally a mistake.
<BODY onload=bancycle ()>


Validate your markup. See <URL:http://validator.w3.or g/>.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
ak******@guidem ail.com (Andy) wrote in message news:<ac******* *************** ****@posting.go ogle.com>...
I'm not sure why my checkbox code is not working as per intended. It
always keeps saying "Please select the department" even though I check
the department... appreciate any help.

I made some comments in the text.

Do not know why [X] by itself wasn't giving an error. I changed them
to just x.

You may want to try out firefox. It has a dom inspector which will
let you see the generated html.

The following one-liner when pasted into the command line will also
display the generated html. (It looks for line ends which are OS
dependent.):

javascript:'<ht ml><head><title >SourceFileList ing<\/title><\/head><body><cod e><ol><li>'+(do cument.document Element||docume nt.body).innerH TML.replace(/&/g,%22&amp;%22). replace(/</g,%22&lt;%22).r eplace(/%20%20/g,%22&nbsp;%20% 22).replace(/\r\n/g,%22<li>%22).r eplace(/\n/g,%22<li>%22).r eplace(/\r/g,%22<li>%22)+' <\/ol><\/code><\/body><\/html>';
Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><TI TLE>Welcome to the Scripting Store</TITLE>
<META http-equiv=Content-Type content="text/html;
charset=windows-1252">
<script type="text/javascript">

// Notice the change in the doctype and script tag.

/* Good that you post your javascript with short lines.
Seems like you have experience with programming.

*/

// Banner Rotator logic

//Load banner elements into array
// Here is another syntax for arrays...

var banners = [ "banner1.jp g",
"banner2.jp g",
"banner3.jp g",
"banner4.jp g" ];

//Set banner counter to 0
var bnrCntr = 0

//Function that actually rotates the banner
function bancycle() {
bnrCntr = bnrCntr + 1
if (bnrCntr == 4) {
bnrCntr = 0
}
document.Banner .src = banners[bnrCntr]
setTimeout("ban cycle()",3000)
}

//End Banner Rotator Logic

</SCRIPT>

<!-- What is this. Be careful about using html generators and
Javascript. Be doubly careful with MS products. -->

<META content="MSHTML 6.00.2800.1106" name=GENERATOR> </HEAD>

<!-- need quote marks around event handler code
The ; is recommended. -->

<BODY onload="bancycl e();">
<TABLE borderColor=blu e width=700 border=1>
<TBODY>
<TR>
<TD width="30%"><IM G name=Banner></TD>
<TD align=middle>
<H1>Welcome to the Scripting
Store</H1></TD></TR></TBODY></TABLE><BR>

<!-- try to minimize javascript inline. Call a function. -->

<SCRIPT language=javasc ript>

document.writel n("Today is: ");
var nDate = new Date();
document.writel n(nDate);

var strName = new String();
strName = window.prompt(" What is your Name?", "FirstNameOnly" );
//document.write( "Welcome " + strName + "<br>");

document.write( "<h2>Hello " + strName + "</h2>");
var varStatus;
status = "Hello " + strName + " - Welcome to the Scripting Store";
//Year(Now());
</SCRIPT>

<!-- Add form tag. -->

<form name="myform">

<!-- I'd move the button below the check boxes. Watch you
eye movement for the why. -->

<P>Please select one of our departments listed below</P><BR><INPUT
onclick=locateD epartment() type=button value="Select Department"
name=btnSelect>
<BR>
<SCRIPT>

var varDept = new Array(3);
varDept[0] = "Books";
varDept[1] = "Computers" ;
varDept[2] = "Caffeine free products";

var numMax = varDept.length;
var strDept;

<!-- I'd go with varDept.length in the code. -->

for (var i=0; i <numMax; ++i)
{

/* Missing signal quotes for value and name.
Sometimes you need the quote marks and sometimes you
can luck out. So, allows do. */

document.write( "<p><input type='checkbox' value='" + i + "' name='" +
varDept[i] + "'>" + varDept[i] + "</p>");
}
</SCRIPT>
</form>
<SCRIPT>

function locateDepartmen t()
{

/* I'm not sure that the loop saves you any code. */

var varselect = true;
var varMax = 3;
for (var x=0; x < varMax; ++x)
{

/* Try puting in alerts for debug. */

alert("x= " + x);

/* Need to reference the form */

if (document.forms["myform"].elements[varDept[x]].checked == true)
{
if (x == 0)
{
location.href=' book.htm';
}
else if (x == 1)
{
location.href=' Caffiene.htm';
}
else if (x == 2)
{
location.href=' Computers.htm';
}
}
else
{
varselect = false;
}
}

if (varselect == false)
{
window.alert("P lease Select a Department");
return false;
}

}

</SCRIPT>
</BODY></HTML>
Jul 23 '05 #6
I got the program to work by changing the input type from checkbox to
radio button. With the checkbox, if two boxes were checked, most
likely there would have been an error because the function could not
goto two links at the same time. (maybe pop-up window is the intention
but not sure).

I also took out the javascript form creation loop and moved the
locateDepartmen t() function to the <HEAD> tag.

HTH!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TI TLE>Welcome to the Scripting Store</TITLE>
<META http-equiv=Content-Type content="text/html;
charset=windows-1252">

<SCRIPT>

function locateDepartmen t()
{

if (document.forms[0].a[0].checked) {
location.href=' book.htm';
return true;

}
else if (document.forms[0].a[1].checked) {
location.href=' Computers.htm';
return true;

}

else {
location.href=' Caffiene.htm';
return true;

}

}

</SCRIPT>
<SCRIPT>

// Banner Rotator logic

//Load banner elements into array
var banners = new
Array("banner1. jpg","banner2.j pg","banner3.jp g","banner4.jpg ")

//Set banner counter to 0
var bnrCntr = 0

//Function that actually rotates the banner
function bancycle() {
bnrCntr = bnrCntr + 1
if (bnrCntr == 4) {
bnrCntr = 0
}
document.Banner .src = banners[bnrCntr]
setTimeout("ban cycle()",3000)
}

//End Banner Rotator Logic

</SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR> </HEAD>
<BODY onload=bancycle ()>

<TABLE borderColor=blu e width=700 border=1>
<TBODY>
<TR>
<TD width="30%"><IM G name=Banner></TD>
<TD align=middle>
<H1>Welcome to the Scripting Store</H1>
</TD>
</TR>
</TBODY>
</TABLE>

<BR>
<SCRIPT language=javasc ript>

document.writel n("Today is: ");
var nDate = new Date();
document.writel n(nDate);

var strName = new String();
strName = window.prompt(" What is your Name?", "FirstNameOnly" );
//document.write( "Welcome " + strName + "<br>");

document.write( "<h2>Hello " + strName + "</h2>");
var varStatus;
status = "Hello " + strName + " - Welcome to the Scripting Store";
//Year(Now());

</SCRIPT>

<P>Please select one of our departments listed below</P>

<BR>

<form>

<INPUT onclick=locateD epartment() type=button value="Select
Department"
name=btnSelect>

<p><input type='radio' name='a' value='a1' checked>Books
<p><input type='radio' name='a' value='b1'>Comp uters
<p><input type='radio' name='a' value='c1'>CAff eine free Products

</form>

</BODY></HTML>
Jul 23 '05 #7

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

Similar topics

0
9687
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
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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
10257
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
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
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
5467
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...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
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.