Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Aaagh. Please help me debug this simple JavaScript code.

Reply
Thread Tools

Aaagh. Please help me debug this simple JavaScript code.

 
 
jeffsnox@gmail.com
Guest
Posts: n/a
 
      08-10-2006
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>

 
Reply With Quote
 
 
 
 
jeffsnox@gmail.com
Guest
Posts: n/a
 
      08-10-2006
Sorry, should have said, the plan is that when you click on CLICK, all
of the checkboxes are checked.

(E-Mail Removed) 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>


 
Reply With Quote
 
 
 
 
Sean Inglis
Guest
Posts: n/a
 
      08-10-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
jeffsnox@gmail.com
Guest
Posts: n/a
 
      08-10-2006
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:
> (E-Mail Removed) 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


 
Reply With Quote
 
tcole6@gmail.com
Guest
Posts: n/a
 
      08-10-2006
You could use Venkeman if you use Firefox. It's a pretty decent
javascript debugger.
(E-Mail Removed) 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:
> > (E-Mail Removed) 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


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      08-10-2006
(E-Mail 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.


--

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      08-10-2006
(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple, simple debug not working Jacob N. Ruby 0 12-29-2010 09:04 PM
Config Mgr Debug/Release and Web.config Compilation debug=true RonL ASP .Net 0 04-08-2006 03:50 PM
Simple simple program error...please help tasheeta@gmail.com C++ 14 11-02-2005 12:52 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments