Show/Hide multiple DIVs with Select using jQuery


Today ill discuss Show/Hide multiple DIVs with Select using jQuery. Bellow a complete example about that

<script type=”text/javascript”>
$(document).ready(function(){
$(‘.box’).hide();
$(‘#dropdown’).change(function() {
$(‘.box’).hide();
$(‘#div’ + $(this).val()).show();
});
});
</script>
<form>
<select id=”dropdown” name=”dropdown”>
<option value=”0″>Choose</option>
<option value=”area1″>DIV Area 1</option>
<option value=”area2″>DIV Area 2</option>
<option value=”area3″>DIV Area 3</option>
</select>
</form>
<div id=”divarea1″>DIV Area 1</div>
<div id=”divarea2″>DIV Area 2</div>
<div id=”divarea3″>DIV Area 3</div>

How to determine if the user is viewing the Joomla front page or not?


If you are using Joomla older version like 1.0 then you’ll only need to verify the current component with the following command:


if ($option == 'com_frontpage' || $option == '') {
echo 'This is the front page';
}

Bellow porsion for Joomla! 1.5 could be easily confirmed by the following command:


if ( JRequest::getVar('view') === 'frontpage' ) {
echo 'This is the front page';
}

Just in case the above command doesn’t work then the issue could be a little more complicated and need to check if the current active menu item is the default one. I saw this issue in some 1.5 early versions.


$menu =& JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'This is the front page';
}

Random password generator with JavaScript


In JavaScript To generate password use the following function.

function generatePW()
{
var i=0;
var s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_?/:(){}[]0123456789";
var pw="";
var c=(Math.random() * 3)+9;
for (i=0; i<c; i++)
{
pw+=s.charAt(Math.random() * 72);
}
return(pw);}

Here the JavaScript function Math.random() generates a random number from 0 to 1 (actually just less than 1). So the code:
var c=(Math.random() * 3)+9; will generate a number between 9 to just less than 12.

OpenCart open source E-commerce solution


OpenCart is an open source PHP-based online shopping cart system. A robust e-commerce solution for Internet merchants with the ability to create their own online business and participate in e-commerce at a minimal cost.

OpenCart is designed feature rich, easy to use, search engine friendly and with a visually appealing interface.

Download link here

How to make jQuery work on the same page with Mootools and or Prototype?


Recently i have faced thats problem on my project. Several of my pages use both JQuery and Protoype. Also i have used Mootools some pages. JQuery this appears to be causing problems, because both libraries define a function named ‘$’.

JQuery provides a function noConflict() which relinquishes control of $ to other libraries that may be using it. So it seems like I need to go through all my pages that look like this:

src=”js/mootools.js”

src=”js/prototype.js”
src=”js/jquery.js”

and change them to look like this:

src=”js/mootools.js”

src=”js/prototype.js”
src=”/obp/js/jquery.js”
jQuery.noConflict();
var $j = jQuery;

I should then be able to use ‘$’ for Prototype and ‘$j’ (or ‘jQuery’) for JQuery. I’m not entirely happy about duplicating these 2 lines of code in every relevant page, and expect that at some point somebody is likely to forget to add them to a new page. I’d prefer to be able to do the following

  • Create a file jquery-noconflict.js which “includes” jquery.js and the 2 lines of code shown above
  • Import jquery-noconflict.js (instead of jquery.js) in all my JSP/HTML pages

An alternate solution is simply to add the 2 lines of code above to jquery.js directly, but if I do that I’ll need to remember to do it every time I upgrade JQuery.

Really Easy Field validation with Prototype


Here’s a form validation script that is very easy to use.

Really Easy Field validation with Prototype

Instructions

The basic method is to attach to the form’s onsubmit event, read out all the form elements’ classes and perform validation if required. If a field fails validation, reveal field validation advice and prevent the form from submitting.

Include the javascript libraries:

You write elements like this:

passing the validation requirements in the class attribute.

You then activate validation by passing the form or form’s id attribute like this:>

new Validation(‘form-id’); // OR new Validation(document.forms[0]);

</p

It has a number of tests built-in but is extensible to include your custom validation checks.

The validator also avoids validating fields that are hidden or children of elements hidden by the CSS property display:none. This way you can give a field the class of ‘required’ but it’s only validated if it is visible on the form. The demo illustrates what I am talking about

Options

Here’s the list of classes available to add to your field elements:

  • required (not blank)
  • validate-number (a valid number)
  • validate-digits (digits only)
  • validate-alpha (letters only)
  • validate-alphanum (only letters and numbers)
  • validate-date (a valid date value)
  • validate-email (a valid email address)
  • validate-url (a valid URL)
  • validate-date-au (a date formatted as; dd/mm/yyyy)
  • validate-currency-dollar (a valid dollar value)
  • validate-selection (first option e.g. ‘Select one…’ is not selected option)
  • validate-one-required (At least one textbox/radio element must be selected in a group – see below*)

*To use the validate-one-required validator you must first add the class name to only one checkbox/radio button in the group (last one is probably best) and then place all the input elements within a parent element, for example a div element. That way the library can find all the checkboxes/radio buttons to check and place the validation advice element at the bottom of the parent element to make it appear after the group of checkboxes/radio buttons.

When the validation object is initialised you can pass the option {stopOnFirst : true} to enable the stop on first validation failure behaiour. The demo above has this set to false which is the default. If set to true only the first validation failure advice will be displayed when the form is submitted instead of all at once.

new Validation(‘form-id’,{stopOnFirst:true});

You can also pass the option {immediate : true} to enable field valiation when leaving each field. That is on the onblur event for all the form elements.

By default the library will add an event listener to the form’s onsubmit event and stop the event if the validation fails. If you pass the option {onSubmit : false} it wont do that. This way you can call the validate function manually within your own javascript.

By default the library will focus on the first field that contains an error. If you pass the option {focusOnError : false} it wont do that.

You can also pass the option {useTitles : true} to make the field validators use the form elements’ title attribute value as the error advice message.

You can set callbacks by using the options {onFormValidate : yourFunction, onElementValidate : yourFunction}.

onFormValidate is called after form validation takes place and takes two arguments: the validation result (true or false) and a reference to the form. OnElementValidate is called after each form element is validated and also takes 2 arguments: the validation result (true or false) and a reference to the form element.

Instead of using the error message in the validator you can create your own validation advice page element. Now when the script is creating the advice element it first looks for an element with an id matching ‘advice-‘ + validation-class-name + ‘-‘ + element.id and if not found then one matching ‘advice-‘ + element.id . If your form element does not have an id attribute then match the name attribute. If it finds an element it will make that one appear. See the ‘Donation’ field in the demo for an example. If you make a custom validation advice element make sure you set the style to display : none .

Also if you reference the effects.js file from Scriptaculous in your page head, it’ll use a fade-in effect for the validation advice.

CSS Hooks

As well as the validation css classes above, the validation library uses CSS classes to indicate validation status:

validation-failed and validation-passed

The validation advice element has a class of validation-advice and an id matching the following pattern

‘advice-‘ + validation-class-name + ‘-‘ + element.id

so if the field ‘ birthdate’ uses the ‘ validate-date’ validation class then the validation advice element will have an id of ‘ advice-validate-date-birthdate’.