SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply JavaScript Book Question

    On page 91 of the Simply Javascript book there's a function on removing a Class from an element.

    Core.removeClass = function(target, theClass)
    {
    var pattern = new RegExp("(^| )" + theClass + "( |$)");

    target.className = target.className.replace(pattern, "$1");
    //this is supposed to replace the class name with an empty string.
    target.className = target.className.replace(/ $/, "");
    //this is to get rid of any spaces that may still be lurking
    };

    Where 'target' is the name of the element
    'theClass' is the name of the class which will be removed.

    My question is:

    "$1" is meant to be an empty string - however it hasn't been initialised anywhere. As in: var $1="";

    Any suggestions...

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the match of the regex.

    For example, /(foo)/ matches and remembers 'foo' in "foo bar." The matched substring can be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9.
    http://developer.mozilla.org/en/docs...nctions:RegExp

    Bye.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for taking the time out to respond.

    Much appreciated


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
  •