SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using dashes in scripts

    Im using a jQuery plugin to validate a form onsubmit. im getting weird errors in firebug ... line per line these seem to disappear if i remove the dashes in the script below. these refer to the name attributes of the form fields being validated. is there a rule in JS that says you can not use dashes?

    Code:
    $("#new-news-form").validate
    	({
    		errorLabelContainer: $("#new-news-form .errors"),
    		rules:
    		{
    			newNewsTitle: "required",
    			new-news-text: "required",
    			new-news-image: {
    				required: true,
    				accept: "jpg|jpeg|gif"
    			},
    			new-news-date: "required"			
    		},
    		messages:
    		{
    			new-news-title: "Please enter a title",
    			new-news-text: "Please enter some text",
    			new-news-image: {
    				required: "please select an image to upload",
    				accept: "Only upload jpg or gif images"
    			},
    			new-news-date: "Please pick a date"
    		}
    	});
    I need someone to protect me from
    all the measures they take in order to protect me

  2. #2
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    confirmed now that all is converted to camelCase it works. is this jQuery specific or does it apply to all JS?
    I need someone to protect me from
    all the measures they take in order to protect me

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you cannot use hyphens in javascript identifiiers-
    new-news is read as new minus news

  4. #4
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks. makes total sense now i think of it ... d'oh
    I need someone to protect me from
    all the measures they take in order to protect me

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    you cannot use hyphens in javascript identifiiers-
    new-news is read as new minus news
    PHP Code:
    <html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    script type="text/javascript" src="jquery/jquery-1.2.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#new-news-form").click(function(){
               alert("Clicked");
            return false;
         })
       
       
     });
    </script>
    </head>
    <body>
    <a href="#" id="new-news-form">click me</a>
    </body>
    </html> 
    It works all in all it's a string.

    Bye.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Consider the following

    Code javascript:
    var obj = {};
    var value = 4;
    obj['a'] = value;
    obj['a-value'] = value;
    alert(obj.a); // 4
    alert(obj.a-value); // 0 ???

    Dashes can be confused for minus signs, so should not be used for identifiers.

    This is why firebug warns you about it, because it's a potential problem. If you're using string notation you can use whatever you like, but it's a bad practice.

    For the official specs, see the JavaScript reference about Member Operators which says:

    property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object.$1 is valid, while object.1 is not.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pmw hit the mark about ids, but there is another issue here.
    which is, what is an identifier?

    id="new-news-form"

    An element's id is the value of an attribute, it is not what is meant by a javascript identifier. An identifier is a variable, property or method name- not the value of any of these objects.

    var hoo=window.document.body.id

    Here, hoo, window,document, body and id are all identifiers-
    but the value of hoo, and of the body's id, are strings.

    You cannot have a var hoo-id, or an object property named
    new-news.

    There are good reasons to use valid identifiers as element ids-
    they make handy property names in objects that group elements,
    for one.

    But if nothing else, it is a sign of competence, like a plumber sweating a clean solder joint.


Bookmarks

Posting Permissions

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