Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post

    Correct syntax query

    Hello

    Can I ask, please, what is the best way of writing the script here (Web form):

    Code:
    <table border="0" cellspacing="1">
    		<tr>
    			<td valign="top">
    				<p class="mySize">Name:</p>
    			</td>
    			<td colspan="2">
    				<input name="NAME" "class=normal" name="ContactUs_Name" size="35" value="<% =ContactUs_Name %>">onblur="toggleColor(this)" onfocus="toggleColor(this)">
    
    			</td>
    In my <head> tag I also have
    Code:
    <STYLE>
    .normal { background-color: #C0D4D2; color: #000000; }
    .focus  { background-color: #9DF7EE; color: #000000; }
    </STYLE>
    
    <SCRIPT LANGUAGE="JavaScript">
    function toggleColor(objElement)
    {
      if (objElement.className=='normal')
        objElement.className='focus';
      else
        objElement.className='normal';
    }
    </SCRIPT>
    followed by:
    Code:
    <BODY onLoad="document.all.NAME.focus()">
    This produces the result on the screenshot I have attached.

    Thanks for any advice.
    Attached Thumbnails Attached Thumbnails Correct syntax query-codeforum.jpg  

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    One of your quotes is out of place, and you gave this input two names. It can have only one (kind of like The Highlander). And you have an extra > that shouldn't be there.
    Code:
    <input name="NAME" class="normal" name="ContactUs_Name" size="35" value="<% =ContactUs_Name %>">onblur="toggleColor(this)" onfocus="toggleColor(this)">
    Also, instead of using document.all, use document.forms["{formname}"].
    Code:
    <BODY onLoad="document.forms["formname"].NAME.focus()">
    ^_^
    Last edited by WolfShade; 04-28-2012 at 10:22 PM.

  • Users who have thanked WolfShade for this post:

    SteveH (04-28-2012)

  • #3
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Many thanks for that Wolfshade - it didn't like

    Code:
    <BODY onLoad="document.forms["formname"].NAME.focus()">
    but the ugly script on the Web page has now gone.

    Cheers

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    If you continue to use document.all your code will only work in MSIE.

    If that doesn't bother you, and you don't care if your JS is obsolescent, then leave it alone.

    Otherwise, find a way to avoid using document.all.

    If you don't have a <form> on the page, then you could do
    Code:
    <body onload="document.getElementsByName("NAME")[0].focus();">
    assuming that you only have one name="NAME" on the page or at least that the one you care about is the first one.

    But now...What does any of this have to do with ASP? It's in the wrong forum. It should be in the JavaScript forum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    SteveH (04-29-2012)

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    750
    Thanks
    138
    Thanked 1 Time in 1 Post
    Oh, I didn't know it was an IE thing. I have a form with only one field with NAME (the name field) and used your suggestion and it seems to work fine.

    Thanks

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Quote Originally Posted by SteveH View Post
    Many thanks for that Wolfshade - it didn't like

    Code:
    <BODY onLoad="document.forms["formname"].NAME.focus()">
    but the ugly script on the Web page has now gone.

    Cheers
    That's because you used a quote inside a quote, truncating everything after the second " . You should either use ' for the onload, or escape the inner " with a backslash. (Sorry I didn't notice this, earlier)

    Code:
    <BODY onLoad='document.forms["formname"].NAME.focus()'>
     . . OR . . 
    <BODY onLoad="document.forms[\"formname\"].NAME.focus()">

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,232
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    No, the backslash trick will *NOT* work.

    Remember, it is HTML that is parsing that, so that it can then invoke JavaScript.

    HTML does *NOT* pay attention to \" as anything special.

    To HTML you would have
    Code:
    <body onload="document.forms[\"     formname\      "].NAME.focus()">
    And it would try to invoke JavaScript with just
    Code:
        document.forms[\
    The first answer is okay, but the preferred way in modern XHTML is
    Code:
    <body onload="document.forms['formname'].NAME.focus()">
    because (a) all tag names and attributes should be lower case and (b) all attribute values should be given in true "..." quotes, not '...'.

    Though even that is obsolescent as named forms are considered out of date. You should, instead, give the form an ID and then do
    Code:
    <body onload="document.getElementById('formid').NAME.focus()">
    Again, this all belongs in the JavaScript forum, rather than here.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •