Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 81,169
Total Posts: 566,839
There are 41 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

jQuery function confusion

Thread title: jQuery function confusion
Reply    
    Thread tools Search this thread Display Modes  
11-14-2010, 10:25 PM
#1
Mizko is offline Mizko
Status: I'm new around here
Join date: Oct 2007
Location: Australia
Expertise:
Software:
 
Posts: 1
iTrader: 0 / 0%
 

Mizko is on a distinguished road

Send a message via MSN to Mizko

  Old  jQuery function confusion

Ok as i've been going through this book i'm learning about basic functions.

I know that this is the basic format for a function.

function functionName() {
$("p").fadeOut("slow");
}

Then you'll have a button to call it out.

But i'm reading about Creating new HTML Elements and in the explample, why is the function set ou tlike this.

$(function()
{
$("<p>Test</p>")
.insertAfter("#first");
});

Just wondering, what is with the $function() could someone please explain, thanks.

11-14-2010, 10:45 PM
#2
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

Originally Posted by Mizko View Post
Ok as i've been going through this book i'm learning about basic functions.

I know that this is the basic format for a function.

function functionName() {
$("p").fadeOut("slow");
}

Then you'll have a button to call it out.

But i'm reading about Creating new HTML Elements and in the explample, why is the function set ou tlike this.

$(function()
{
$("<p>Test</p>")
.insertAfter("#first");
});

Just wondering, what is with the $function() could someone please explain, thanks.
It is a matter of timing, normal JS functions run before the HTML is ready to be messed with. When it is in there jQuery waits until everything is good to go.

11-22-2010, 02:12 AM
#3
Enfernikus is offline Enfernikus
Status: I'm new around here
Join date: Jul 2008
Location:
Expertise:
Software:
 
Posts: 20
iTrader: 1 / 100%
 

Enfernikus is on a distinguished road

  Old

Code:
$(function(){
});
Is short hand for

Code:
jQuery(document).ready(function(){
});
What the ready method does, succinctly, is use cross-browse compatible methods to ensure that the element you've specified has been loaded fully and is in a "ready" state to be manipulate. We choose to the document element because it represents the whole of the page we're working with.

Reply With Quote
Reply    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed