Solution for multiple document.ready() function | Lessoncup Programming Blog

Monday, 28 October 2013

Solution for multiple document.ready() function

In common way developers will write multiple document.ready() functions in same page and they wont care about code coverage, as a programmers i am not talking about all programmers those who still following, those should concentrate to reduce the code as much as possible and it is very good practice to keep all functions in one group and put it in one file.


now i am showing you one example with multiple document.ready() functions

<script>

$(document).ready(function(){

$('#btn').click(function(){
alert('hello')
}
});

$(document).ready(function(){

$('#btn1').click(function(){
alert('hello');
}
});
</script>


as above example we can write without using multiple document.ready() functions, see the bellow example

<script> $(function(){ $('#btn').click(function(){ alert('hello') } $('#btn1').click(function(){ alert('hello'); }
});
</script>

here a function passed as an argument to jQuery constructor and it is bound to 
the document ready event.

No comments :

Post a Comment

Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA
lessoncup@gmail.com

PHP | My SQL | JQUERY
Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons