473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Aaagh. Please help me debug this simple JavaScript code.

Here's the code which I'm saving in an HTML and opening in a Java
enabled browser. It's giving me the following error:

Line: 34
Char: 1
Error: Object Expected

Many thanks in advance for any help.

Code follows:
============================
<html>
<head>
<script language="javascript">

function chkSelectAll(strMatch,frm)
{
for (var i=0; i < frm.elements.length; i++)
{
var form_field = frm.elements[i];
var field_name = form_field.name;

if (field_name.substring(0,strMatch.length)) == strMatch)
{
if (form_field.type == "checkbox")
{
form_field.checked = true;
}
}
}
}

</script>
</head>
<body>

<form name='bsForm'>
<input type='checkbox' name='chkbox1' />
<input type='checkbox' name='chkbox2' />
<input type='checkbox' name='chkbox3' />
<input type='checkbox' name='chkbox4' />
<input type='checkbox' name='chkbox5' />
</form>

<a href='#' onClick='chkSelectAll("chkbox",document.bsForm);'> CLICK</a>

</body>
</html>

Aug 10 '06 #1
5 1282
Sorry, should have said, the plan is that when you click on CLICK, all
of the checkboxes are checked.

jeffs...@gmail.com wrote:
Here's the code which I'm saving in an HTML and opening in a Java
enabled browser. It's giving me the following error:

Line: 34
Char: 1
Error: Object Expected

Many thanks in advance for any help.

Code follows:
============================
<html>
<head>
<script language="javascript">

function chkSelectAll(strMatch,frm)
{
for (var i=0; i < frm.elements.length; i++)
{
var form_field = frm.elements[i];
var field_name = form_field.name;

if (field_name.substring(0,strMatch.length)) == strMatch)
{
if (form_field.type == "checkbox")
{
form_field.checked = true;
}
}
}
}

</script>
</head>
<body>

<form name='bsForm'>
<input type='checkbox' name='chkbox1' />
<input type='checkbox' name='chkbox2' />
<input type='checkbox' name='chkbox3' />
<input type='checkbox' name='chkbox4' />
<input type='checkbox' name='chkbox5' />
</form>

<a href='#' onClick='chkSelectAll("chkbox",document.bsForm);'> CLICK</a>

</body>
</html>
Aug 10 '06 #2

je******@gmail.com wrote:
Here's the code which I'm saving in an HTML and opening in a Java
enabled browser. It's giving me the following error:

Line: 34
Char: 1
Error: Object Expected

Many thanks in advance for any help.

Code follows:
============================
<html>
<head>
<script language="javascript">

function chkSelectAll(strMatch,frm)
{
for (var i=0; i < frm.elements.length; i++)
{
var form_field = frm.elements[i];
var field_name = form_field.name;

if (field_name.substring(0,strMatch.length)) == strMatch)
{
if (form_field.type == "checkbox")
{
form_field.checked = true;
}
}
}
}

</script>
</head>
<body>

<form name='bsForm'>
<input type='checkbox' name='chkbox1' />
<input type='checkbox' name='chkbox2' />
<input type='checkbox' name='chkbox3' />
<input type='checkbox' name='chkbox4' />
<input type='checkbox' name='chkbox5' />
</form>

<a href='#' onClick='chkSelectAll("chkbox",document.bsForm);'> CLICK</a>

</body>
</html>
Your problem is:

if (field_name.substring(0,strMatch.length)) == strMatch)

if you look carefully, you have an extra ")" after strMatch.length.
This means the JS doesn't parse, so the function is unavailable when
you try to use it.

I prefer:

if (field_name.indexOf(strMatch) == 0)

myself, but YMMV

Aug 10 '06 #3
Thank you! Can't believe I missed that. Doh!

I wish the error messages were a little more detailed in JavaScript. I
normally code in other languages and always feel like I'm stumbling
around in the dark with JS.

Thanks again.

Sean Inglis wrote:
je******@gmail.com wrote:
Here's the code which I'm saving in an HTML and opening in a Java
enabled browser. It's giving me the following error:

Line: 34
Char: 1
Error: Object Expected

Many thanks in advance for any help.

Code follows:
============================
<html>
<head>
<script language="javascript">

function chkSelectAll(strMatch,frm)
{
for (var i=0; i < frm.elements.length; i++)
{
var form_field = frm.elements[i];
var field_name = form_field.name;

if (field_name.substring(0,strMatch.length)) == strMatch)
{
if (form_field.type == "checkbox")
{
form_field.checked = true;
}
}
}
}

</script>
</head>
<body>

<form name='bsForm'>
<input type='checkbox' name='chkbox1' />
<input type='checkbox' name='chkbox2' />
<input type='checkbox' name='chkbox3' />
<input type='checkbox' name='chkbox4' />
<input type='checkbox' name='chkbox5' />
</form>

<a href='#' onClick='chkSelectAll("chkbox",document.bsForm);'> CLICK</a>

</body>
</html>

Your problem is:

if (field_name.substring(0,strMatch.length)) == strMatch)

if you look carefully, you have an extra ")" after strMatch.length.
This means the JS doesn't parse, so the function is unavailable when
you try to use it.

I prefer:

if (field_name.indexOf(strMatch) == 0)

myself, but YMMV
Aug 10 '06 #4
You could use Venkeman if you use Firefox. It's a pretty decent
javascript debugger.
je******@gmail.com wrote:
Thank you! Can't believe I missed that. Doh!

I wish the error messages were a little more detailed in JavaScript. I
normally code in other languages and always feel like I'm stumbling
around in the dark with JS.

Thanks again.

Sean Inglis wrote:
je******@gmail.com wrote:
Here's the code which I'm saving in an HTML and opening in a Java
enabled browser. It's giving me the following error:
>
Line: 34
Char: 1
Error: Object Expected
>
Many thanks in advance for any help.
>
Code follows:
============================
<html>
<head>
<script language="javascript">
>
function chkSelectAll(strMatch,frm)
{
for (var i=0; i < frm.elements.length; i++)
{
var form_field = frm.elements[i];
var field_name = form_field.name;
>
if (field_name.substring(0,strMatch.length)) == strMatch)
{
if (form_field.type == "checkbox")
{
form_field.checked = true;
}
}
}
}
>
</script>
</head>
<body>
>
<form name='bsForm'>
<input type='checkbox' name='chkbox1' />
<input type='checkbox' name='chkbox2' />
<input type='checkbox' name='chkbox3' />
<input type='checkbox' name='chkbox4' />
<input type='checkbox' name='chkbox5' />
</form>
>
<a href='#' onClick='chkSelectAll("chkbox",document.bsForm);'> CLICK</a>
>
</body>
</html>
Your problem is:

if (field_name.substring(0,strMatch.length)) == strMatch)

if you look carefully, you have an extra ")" after strMatch.length.
This means the JS doesn't parse, so the function is unavailable when
you try to use it.

I prefer:

if (field_name.indexOf(strMatch) == 0)

myself, but YMMV
Aug 10 '06 #5
je******@gmail.com wrote:
<snip>
I wish the error messages were a little more detailed in
JavaScript. I normally code in other languages and always
feel like I'm stumbling around in the dark with JS.
<snip>

If you are using IE you should set its error report dialog to always
open when there is an error, then it would have popped open when the
syntax error happened. Failing that (and anyway) there is the 'previous'
button on the bottom of the dialog. Use it to go back through any
sequence of errors, as it is most often the first one that causes those
that follow.

(and please do not top-post to comp.lang.javascript)

Richard.
Aug 10 '06 #6

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

Similar topics

1
by: Robert | last post by:
Simple way of writing debug print statements in your JavaScript code. You need to have enabled popups in your browser. I liked the ability to write to the java console in Netscape 4.x. This,...
2
by: | last post by:
Help! I'm new to c++, and am breaking my teeth on MS Visual C++ (bundled within Visual Studio .NET 2003). Am trying to link simple c++ code to fortran dlls created in Compaq Visual Fortran (v6.1)....
3
by: Kris van der Mast | last post by:
Hi, I've created a little site for my sports club. In the root folder there are pages that are viewable by every anonymous user but at a certain subfolder my administration pages should be...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
11
by: samuelberthelot | last post by:
Hi, I've got 3 input HTML (dropdown lists) on my page. One for selecting a Month, one for the day, one for the year. Very simple... My problem is that I'd like to update the Days one according...
4
by: TARUN | last post by:
Hello all I am new to web technology, so plz excuse me for posting such a silly question . I have a code in C#.NET , ASP.NET, Javascript . I am a begginer of Javascript i don't understand...
4
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have a simple user control that has a button on it which is on my web page. everytime i click it i get the following message: --------------------------- Error...
112
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.