Results 1 to 3 of 3

Thread: $ sign as shortcut for getelementbyid

  1. #1
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default $ sign as shortcut for getelementbyid

    Can anyone tell me why this won't work?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function $(element) {
      return document.getElementById(element);
    }
    function bob() {
    var x = $('text1');
    alert(x);
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="bob()">Hello</a>
    <input type="text" value="54321" id="text1">
    </body>
    </html>
    Last edited by keyboard; 02-21-2012 at 01:23 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,978
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Works fine here. In Firefox I get:

    [object HTMLInputElement]
    In IE I get:

    [object]
    Both of which are accurate. If you were expecting the element's value, then:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function $(element) {
      return document.getElementById(element);
    }
    function bob() {
    var x = $('text1').value;
    alert(x);
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="bob()">Hello</a>
    <input type="text" value="54321" id="text1">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Ahh, that explains it. Thanks jscheuer1!

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
  •