Archive for the ‘jQuery’ Category

jQuery: jQuery event with RaphaelJs

Friday, February 5th, 2010

HTML

<div id="rtest">
</div>

Javascript code

<script type="text/javascript">
 $(document).ready(function(){
	var paper = Raphael("rtest", 320, 200);
	var circle = paper.circle(50, 40, 30);
	circle.attr({
  	 "stroke": "#336699",
	  "fill": "#fbfbfb"
	});

	$(circle.node).click(function(){
		var circle = this.raphael;

                //Toggle the circle fill color
                if (circle.attr("fill") == "#333333") {
		     circle.attr("fill", "#fbfbfb");
                } else {
		     circle.attr("fill", "#333333");
		}
	});
});
</script>

See RaphaelJs Event doc

jQuery: Input Focus and Select

Monday, February 1st, 2010

This will focus and select the text of a given input. Nice to help the user doing the most likely action.

$("input[name='First Name']").focus().select();

jQuery: bubbleBind jQuery Plugin to bind bubbling events

Monday, January 18th, 2010

UPDATE 2010-02-26: JQuery 1.4.2 added the delegate method, which does exactly that. Use the jQuery.delegate rather than bubbleBind (I just kepp the code below for reference)

bubbleBind is a jQuery plugin to conveniently bind bubbling events for given child targets. It is very similar to jQuery.bind and uses it under the cover (as well as jQuery.is()). Nothing magic, just simple, convenient, and familiar.

Example

<div id="parentDiv">
  <span>one</span>
  <span>two</span>
</div>
$("#parentDiv").bubbleBind("span","click",function(){
            alert("you clicked on an span tag: " + $(this).html());
});

Plugin code

License free: use as you want, modify as you can, attribute as you feel.

(function($) {

  /**
   * Allow to bind a bubbled-event at the parent level for a given child (matching the childSelector).
   * The selected element(s) will be considered the parent(s), and the childSelector will filter the target element.
   * If, an only if, the target match the childSelector, then, the handler will be called. The key work "this" will
   * point to the target document element (as in jQuery bind)
   *
   * Example:
   * 	$("#parentDiv").bubbleBind("span","click",function(){
   *            alert("you clicked on an span tag: " + $(this).html());
   *    });
   *
   * @param {String} childSelector the jQuery selector for the targeted child. This will be use in $target.is(childSelector)
   * @param {String} a jQuery evenType (same a jQuery bind)
   * @param {Function} the handler function
   *
   * TODO: need to support eventData
   */
  $.fn.bubbleBind = function(childSelector,eventType,handler) {

      // iterate and reformat each matched element
      return this.each(function() {
  		  var $parentElement = $(this);

  		  //bind the even to the parent
  		  $parentElement.bind(eventType,function(event){

  		  	//get the target and test the target against the childSelector
  		  	var $target = $(event.target);
    			if ($target.is(childSelector)){
    				// If matched, call the handler method using the target html element as reference
    				// Note: Might be a smarter and more conventional way to do this. Feel free to suggest.
    				$target.get(0).bubbleBindHandler = handler;
    				$target.get(0).bubbleBindHandler(event);
    			}
  		  });	 				

      }); 

   }; 

})(jQuery);

jQuery: Plugin Skeleton

Monday, January 18th, 2010

Should be in file jquery.samplePlugin.js

(function($) {

  /**
   * Skeleton for jQuery plugin
   * @param {Object} options
   */
  $.fn.samplePlugin = function(options) {

      // extends options with the default one
      var opts = $.extend({}, $.fn.samplePlugin.defaults, options);

      // iterate and process each matched element
      return this.each(function() {
          var $this = $(this); // jQuery object for this element
          //do what is needed
      }); 

   }; 

  // samplePlugin default options
  $.fn.samplePlugin.defaults = {};

})(jQuery);

JQuery: remove animation for jQuery UI date picker

Friday, November 27th, 2009
$("#screenerDatePickers input").datepicker({ duration: '' });

source

JQuery: remove table column

Friday, November 27th, 2009
 var colIdx = 2;
$('#myTable tr td:nth-child('+ colIdx  +'),
   #myTable tr th:nth-child('+ colIdx  +')').remove();

Source

jQuery: Drag custom Clone helper with the correct width

Wednesday, November 11th, 2009

Often, when you drag an element with the helper = ‘clone’, you want the clone element width to be equal to the draggable element width. Unfortunately, this works only if the “width” has been set to the draggable element.

The solution, is to create a custom method for the helper like:

$(”.draggableElement”).draggable({
helper: function(){
return $(this).clone().width($(this).width());
},
revert: “invalid”
});

jQuery: iFrame find elements

Monday, July 20th, 2009
$(’#iframeID’).contents().find(’#someID’).html();

Source: http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/

jQuery: Ajax get with callback

Tuesday, July 14th, 2009
$.get("test.cgi", { name: "John", time: "2pm" },
     function(data){
      alert("Data Loaded: " + data);
});

jQuery AJAX Get Doc

JQuery: Keypress enter key

Saturday, July 11th, 2009
$("input").keypress(function (e) {
  if (e.which == 13){
     alert("You have pressed Enter");
  }
}

See also: