News Archive
Related Articles
PhpRiot Newsletter
Your Email Address:

More information

5 Useful jQuery Snippets for your Website

Note: This article was originally published at Planet PHP on 24 August 2010.
Planet PHP

jQuery is a popular JavaScript library which is used by many developers and applications. While using jQuery you need to write less code, writing functions is less complex and there are a lot of plugins you can use for free in your web application. Even if you code everything by yourself, you need only a few rows of code to create nice and powerful features for your website. If you're in hurry check the jQuery demo page.

Populate select menus with jQuery and Ajax

One of the most powerful jQuery function is the Ajax Suite. This example shows how-to populate a second select menu based on the option from the first select menu.

Just in case we have this select menu with the main categories:

form p label for="category"Choose: /label select name="category" id="category" option value="fruit"Fruit/option option value="grain"Grains/option option value="vegetables"Vegetables/option /select span id="subcat"/span /p /form

If someone has changed the value for this select menu, we want to show the equivalent menu inside the span element with the ID aosubcata. To do this we use this jQuery snippet that makes an Ajax request to a PHP file called aogetSubCat.phpa. After the select menu is changed a loading image will show up (in case of slow Internet connections), the PHP script is called and the sub-select menu becomes visible.

$(document).ready(function() { $('#category')."/

Truncated by Planet PHP, read more at the original (another 31604 bytes)