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 5 of 5
  1. #1
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Dollar sign explanation

    Could someone please explain this bit of code for me, and perhaps paste of couple of links to tutorials that deal with this sort of thing:
    Code:
    remove: function(element) {
        element = $(element);
    Also, if anyone has experience using the Prototype Javascript Framework, I'd appreciate any help in understanding it.
    Last edited by hemebond; 08-15-2005 at 03:17 AM.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    remove is the referencable name of the function. You can name any javascript objects by using a colon after a name, useful for things like this (loop object naming):
    Code:
    var a, b;
    outer_loop:
    for(a=0; a<5; a++) {
      for(b=0; b<5; b++) {
        if(a == 1 && b == 3) {
          break outer_loop;
        }
      }
    }
    or like this (child object naming):
    Code:
    var Prototype = {
      Version: '1.3.1', // Prototype object Version object is string '1.3.1'
      emptyFunction: function() {} // Prototype object emptyFunction object is an empty anonymous function
    }
    The function() bit is just declaring an anonymous function passing an parameter "element". The usage of a single $ sign usually denotes the input of an regular expression - it's an attribute of the RegExp object.
    However in this case it's actually a function called $:
    Code:
    function $() {
      var elements = new Array();
    
      for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
          element = document.getElementById(element);
    
        if (arguments.length == 1) 
          return element;
    
        elements.push(element);
      }
    
      return elements;
    }
    This function basically returns the html element object(s) for the id string(s) you pass to the $ function. If you only pass one element id in, then it just returns the one html element object, otherwise if more than one id string is passed in, it returns an array of html element objects.
    usage:
    Code:
    var obj = $("myDiv"); // same as doing var obj = document.getElementById("myDiv");
    var objs = $("myDiv1", "myDiv2"); 
    // same as: var objs = new Array(document.getElementById("myDiv1"), document.getElementById("myDiv2"));
    Hope this helps

    m_n
    Last edited by martin_narg; 08-15-2005 at 03:58 AM.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by martin_narg
    The function() bit is just declaring an anonymous function passing an parameter "element". The usage of a single $ sign denotes the input of an regular expression - it's an attribute of the RegExp object.
    Thanks Martin. I should have removed the remove: as I understand that part, It's just the usage of the $ sign I don't understand. I've never seen it used before. You can get the whole script via the link on my original post.

    Update: Awesome, thanks Martin. I've been hunting around all day trying to find out what it means. I now remember seeing the $ function, but I thought it was a reference or something. Bah! Dang obscure code.
    Last edited by hemebond; 08-15-2005 at 03:38 AM.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    cool, pleased to help
    Last edited by martin_narg; 08-15-2005 at 03:46 AM.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Dollar Sign

    4 years later, in jQuery the dollar sign has a special meaning. There is an article written about it on the following url: Explanation of the Javascript Dollar Sign ($)


  •  

    Posting Permissions

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