TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   $(element) (http://www.talkphp.com/javascript-ajax-e4x/2265-element.html)

Orc 02-17-2008 04:48 AM

$(element)
 
What does it mean to do that in javascript? I tried finding it on google, but got no good results.

javascript Code:
$(element)

Salathe 02-17-2008 05:38 AM

It's common for many JavaScript libraries to make use of a 'dollar function' as a convenient shortcut for accessing DOM elements (elements in your HTML page). Different libraries do things slightly differently so it's best to look up in the appropriate documentation pages for any particular library that you're using.

It's not a native JavaScript function! Also Google's pretty rubbish at searching for (admittedly very vague) things like that. :)

Orc 02-17-2008 05:40 AM

Quote:

Originally Posted by Salathe (Post 10905)
It's common for many JavaScript libraries to make use of a 'dollar function' as a convenient shortcut for accessing DOM elements (elements in your HTML page). Different libraries do things slightly differently so it's best to look up in the appropriate documentation pages for any particular library that you're using.

It's not a native JavaScript function! Also Google's pretty rubbish at searching for (admittedly very vague) things like that. :)

Does Prototype contain this method?

DeMo 02-17-2008 06:18 AM

Yes Orc, prototype is one of the many JS libraries that work with a syntax like that.

For instance, if you need to get the value of a form field you could do it like this:
Code:

var x = document.getElementById("field_id").value;
With prototype you can do this by typing:
Code:

var x = $F("field_id");
Ultimately, prototype will simply "forward" your call to document.getElementById("..").value. But since it is a cross-browser library it will also do some browser checks and if it can't retrieve the value using document.getElementById("..").value it will use another method. Prototype frees you from doing this dirty work of browser checks and stuff like that, it also let's you write smaller and cleaner code. On the other hand, prototype is over 100k in size, keep in mind that it will be included in your pages and your visitors browser will have to download it, the good news is that the browser will probably cache it.. so you don't need to worry about it slowing the page loading time. Check the prototype documentation for more info.

Other libraries (like jQuery) also use the $() syntax. In jQuery you can use regular expressions and a lot of other stuff inside the $() that allows you to select and apply effects to various DOM elements at the same time, it's an interesting library too.

Orc 02-17-2008 06:21 AM

Quote:

Originally Posted by DeMo (Post 10910)
Yes Orc, prototype is one of the many JS libraries that work with a syntax like that.

For instance, if you need to get the value of a form field you could do it like this:
Code:

var x = document.getElementById("field_id").value;
With prototype you can do this by typing:
Code:

var x = $F("field_id");
Ultimately, prototype will simply "forward" your call to document.getElementById("..").value. But since it is a cross-browser library it will also do some browser checks and if it can't retrieve the value using document.getElementById("..").value it will use another method. Prototype frees you from doing this dirty work of browser checks and stuff like that, it also let's you write smaller and cleaner code. On the other hand, prototype is over 100k in size, keep in mind that it will be included in your pages and your visitors browser will have to download it, the good news is that the browser will probably cache it.. so you don't need to worry about it slowing the page loading time. Check the prototype documentation for more info.

Other libraries (like jQuery) also use the $() syntax. In jQuery you can use regular expressions and a lot of other stuff inside the $() that allows you to select and apply effects to various DOM elements at the same time, it's an interesting library too.

jQuery sounds fun. :D

xenon 02-17-2008 10:38 AM

Prototype does that, too (and more).

Code:

$('element_id');
$('element_name');
$('element_class_name');
$('element_tag_name');

All work with prototype. I wrote a function to use in my scripts that don't use prototype, but I needed the simplicity of it:

Code:

var $ = function(element)
{
    if( document.getElementById(element) )
    {
        return document.getElementById(element);
    }
    else if( document.getElementsByTagName(element) )
    {
        return document.getElementsByTagName(element);
    }
    else if( document.getElementsByTagName(element) )
    {
        return document.getElementsByTagName(element);
    }
   
    return null;
}


Morishani 02-24-2008 03:25 PM

Prototype do more (I think), you can put in it some css phrases like :
Quote:

$("ul li.file")
and it will return all the elements that matches to the css phrase.

ubie 03-07-2008 11:40 PM

in prototype ..
$(element) just like document.getElementById()am i right??


All times are GMT. The time now is 06:44 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0