TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-17-2008, 04:48 AM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default $(element)

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

javascript Code:
$(element)
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-17-2008, 05:38 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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. :)
Salathe is offline  
Reply With Quote
Old 02-17-2008, 05:40 AM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-17-2008, 06:18 AM   #4 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

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.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
The Following User Says Thank You to DeMo For This Useful Post:
Orc (02-17-2008)
Old 02-17-2008, 06:21 AM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by DeMo View Post
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
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 02-17-2008, 10:38 AM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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;
}
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
SOCK (02-17-2008)
Old 02-24-2008, 03:25 PM   #7 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

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.
__________________
מטבחים (hebrew)
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
The Following User Says Thank You to Morishani For This Useful Post:
Orc (03-02-2008)
Old 03-07-2008, 11:40 PM   #8 (permalink)
The Wanderer
 
ubie's Avatar
 
Join Date: Mar 2008
Location: malang, Indonesia
Posts: 14
Thanks: 0
ubie is on a distinguished road
Default

in prototype ..
$(element) just like document.getElementById()am i right??
Send a message via Yahoo to ubie
ubie is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:27 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design