JavaScript

Replace getElementById JavaScript function by using jQuery selector. Your old getElementById code: document.getElementById(’unique_id’).doSomething(); Your new jQuery code: jQuery(’#unique_id’).doSomething();

jQuery one

In: JavaScript

7 Jul 2010

Use jQuery one to attach event that will be executed only once. 1 2 3 <a id="jQueryOne" href="javascript:void(0);" title="jQuery one"> Tell me once! </a> 1 2 3 jQuery(’#jQueryOne’).one(’click’, function() { alert(’jQuery one: "I shall say this only once".’); });

To load jQuery from Google, use following snippet: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

You can use jQuery function load() to load external page. Therefore, you can easily make dynamically loaded content on your website. Let’s say you want to load page “content.php” into div on your page. A whole page would contain next snippet: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [...]

JS include

In: JavaScript|PHP

6 Jul 2010

In order to lower down number of http requests and speed up your website, you can use regular JS include, to include PHP file. <link rel="javascript" type="text/javascript" href="alljs.php"> PHP JS include file This is how PHP file, that includes all JS files you want to be included, should look on your server: 1 2 3 [...]

jQuery add class

In: JavaScript

1 Jul 2010

To add class to HTML element using jQuery, you should be using jQuery function addClass(). By using a selector to match elements on HTML and calling function .addClass(‘className’); you can add class to the specific element. See example: 1 jQuery(’#element_id’).addClass(’className’);

You can submit form using jQuery easily using .submit() jQuery function, just like in this snippet: jQuery(’#form_id’).submit();

You can use jQuery focus on element in order to set focus upon element or in order to check if there is focus on element? In order to set focus on element use: 1 jQuery(’#element_id’).focus(); In order to check if there is focus on element use: 1 2 3 if(jQuery(’#element_id’).is(":focus")) { //fixed thanks to [jquery [...]

jQuery find tag

In: JavaScript

29 Jun 2010

If you want to find specific tag with jQuery you can use jQuery selector. Let’s say you would like to find all div elements with class=”handle”, and you would like to remove them. Example shows how: jQuery(’div[class=handle]‘).remove();

If you want to have nice, “go to top” button with sliding to top effect on your webpage, just copy following code: 1 2 3 <a onclick="jQuery(‘html, body’).animate( { scrollTop: 0 }, ‘slow’ );" href="javascript:void(0);"> Go to top </a> If you would like to call slide “go to top” from your javaScript function, just use: [...]