Posts Tagged ‘js’

How to disable some of the options in an HTML select using jQuery

Wednesday, April 15th, 2009

Sometimes you don't need users to select some of the options in the HTML drop down menu.

For example you have a dropdown menu with these options.

==========================
Please select a province/state for US/Canada

Canada
--------------
Prov1
...
Prov10

United States
--------------
State1
...
State50
==========================

What we need is a province/state but what if the user selects the separators '----' ? or the country name ?

To solve this challenge we use negative values for values that should not be selected. Then using jQuery we disable them.

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#acc_info select option").each(function (index, obj) {
// Disable items that have a value less then 0 OR an empty string
// We want those elementes to be disabled only.
if (jQuery(obj).val() == '' || jQuery(obj).val() <= 0) {
jQuery(obj).attr("disabled", "disabled");
}
});
});
</script>

Related:

How to detect if your site or current page has been opened in a frame (such as Google images) ?

Wednesday, March 18th, 2009

The following cool code is supposed to redirect if your site has been opened in a frame.

<script type="text/javascript" language="JavaScript">
var topLocation
try {topLocation = top.location.href; }
catch(er) { window.top.location.href = self.location.href;}
</script>

Credits: http://www.mydreamsbook.com

How did I find this ?

I was looking for Elizabeth Banks pictures :D (She's so beautiful by the way).
She is in Zack and Miri Make a Porno film which by the way I didn't like so much but that's another topic.
Related

How to implement a timed effect with jQuery

Tuesday, March 10th, 2009

By a timed effect here I mean do some animation and then redirect the user to a page.
Requirements: jQuery (and Javascript)
Download the file: timed_effect_jquery

Here is how to accomplish that:

Implementation

/*
This function performs some effects on a target_obj and after 2.5 sec redirects to a location defined by ahref_obj
Usage: in shopping carts e.g. when someone adds a product then some animation is perfomed to make the user aware that something is happening.
License: LGPL
Author: Svetoslav Marinov
Site: http://slavi.biz
*/
function my_timed_effect(target_obj, ahref_obj) {
jQuery(target_obj).attr('style', 'font-weight:bold;').fadeOut("slow").fadeIn("slow").show('slow');
setTimeout('document.location="' + ahref_obj.href + '"', 2000);
}

Usage:

<a href="/cart/add/1" onclick="my_timed_effect(jQuery('#my_status_container_id'), this); return false;">Add a product</a> <br />
<a href="http://google.com" onclick="my_timed_effect(jQuery('#my_status_container_id'), this); return false;">Add a product</a> (go to http://google.com after the animation)

This example will perform the animation and then redirect where the link points.

How would you do the same effect ?
Are there any other option(s) ?

How to disable form submission when someone hits the enter key

Saturday, February 28th, 2009

Some times you want to disable the form submission when someone hits the enter key.
Here is how to do it.

// Ignores the enter key to prevent accidental form submission.
// Usage: <input class="" type="text" id="filter" name="filter" value="" onKeyPress="return my_ignore_enter(event);" />
// src: http://jennifermadden.com/javascript/stringEnterKeyDetector.html
// src: http://www.w3schools.com/jsref/jsref_onkeypress.asp
function my_ignore_enter(e) {
var characterCode;

if (window.event) { // IE
characterCode = e.keyCode;
} else if(e.which) { // Netscape/Firefox/Opera
characterCode = e.which;
}

if (characterCode == 13) {
return false;
}

return true;
}

Related

How to extract id/numbers from the end of an URL using Javascript Regular Expressions (Regex)

Wednesday, February 11th, 2009

I was working with the Zend Framework and flowplayer and I needed to extract the ID from the url.
Here is the short JavaScript snippet.

var extr_id_regex = new RegExp('(\\d+)$', "gi");
var url = '/a/b/c/123';
var id = url.match(extr_id_regex);

if (id) {
alert(id);
}

Related