Aaagh. Please help me debug this simple JavaScript code.

Discussion in 'Javascript' started by jeffsnox@gmail.com, Aug 10, 2006.

  1. Guest

    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>
     
    , Aug 10, 2006
    #1
    1. Advertising

  2. Guest

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

    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;
    > 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, 2006
    #2
    1. Advertising

  3. Sean Inglis Guest

    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;
    > 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
     
    Sean Inglis, Aug 10, 2006
    #3
  4. Guest

    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:
    > 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;
    > > 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, 2006
    #4
  5. Guest

    You could use Venkeman if you use Firefox. It's a pretty decent
    javascript debugger.
    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:
    > > 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;
    > > > 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, 2006
    #5
  6. Lee Guest

    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.


    --
     
    Lee, Aug 10, 2006
    #6
  7. 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.
     
    Richard Cornford, Aug 10, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    569
    Chris Uppal
    May 5, 2005
  2. Asfand Yar Qazi
    Replies:
    2
    Views:
    551
    Asfand Yar Qazi
    Sep 26, 2003
  3. KK
    Replies:
    2
    Views:
    728
    Big Brian
    Oct 14, 2003
  4. Replies:
    14
    Views:
    558
    Karl Heinz Buchegger
    Nov 2, 2005
  5. Jacob N.
    Replies:
    0
    Views:
    102
    Jacob N.
    Dec 29, 2010
Loading...

Share This Page