Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2012, 02:30 AM   PM User | #1
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 789
Thanks: 208
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Question Question about accessing DOM via jQuery

Hi

Can someone please tell me why does the following code snippet does NOT work

http://jsbin.com/izuzud/1/edit

But the following works?

http://jsbin.com/efujar/1/edit

The code is exactly the same same except the position.
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 10-23-2012, 09:40 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,585
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
This is because you are executing the script before the element is even existing. If you include the script in the head you need to wait for the DOM to be loaded. Have you confused the self-invoking function with the jQuery DOM ready function or why are you using it?

PHP Code:
$(function(){
  $(
'#mybutton').click(function(){
    var 
= $('#mydiv').text();
    
alert(a);
  });
}); 
The self-invoking function isn’t even necessary, if you put the script at the end of the document you can even do
PHP Code:
<script type="text/javascript">
$(
'#mybutton').click(function(){
  var 
= $('#mydiv').text();
  
alert(a);
});
</script> 
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 10-23-2012, 09:46 AM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 530 Times in 524 Posts
devnull69 will become famous soon enough
Both code samples contain the following construct
Code:
(function() {
   ...
})(jQuery);
which doesn't make any sense in that case. It creates an anonymous function with no parameter which will be immediately called with the parameter jQuery. The parameter will not be matched to anything so it will be lost.

The position of the code matters in that case because the function will be executed immediately and the element '#mybutton' will only exist before the end of the <body>

This is probably what you wanted to do instead
Code:
$(function() {
   ...
});
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:26 AM.


Advertisement
Log in to turn off these ads.