Aaagh. Please help me debug this simple JavaScript code.

J

jeffsnox

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;
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>
 
J

jeffsnox

Sorry, should have said, the plan is that when you click on CLICK, all
of the checkboxes are checked.
 
S

Sean Inglis

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;
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
 
J

jeffsnox

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 said:
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;
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
 
T

tcole6

You could use Venkeman if you use Firefox. It's a pretty decent
javascript debugger.
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 said:
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;
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
 
L

Lee

(e-mail address removed) said:
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.

Test in Firefox. The error messages are much more helpful.


--
 
R

Richard Cornford

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top