
Untitled
By: a guest on
Jul 22nd, 2012 | syntax:
None | size: 0.76 KB | hits: 15 | expires: Never
how to attach/extend methods to dom element
<div id='hi1'> aaa </div>
$('#hi1').sayHi();
$.fn.sayHi = function() {
alert($(this).text());
return this;
};
$('#hi1').sayHi();
$.fn.extend({ sayHi:function(){ alert('hi'); });
jQuery.fn.sayHi = function() {
// this is the jQuery object
// so this[0] is a DOM element (or undefined)
};
$.fn.sayHi = function(something){
alert(something);
};
$('#hi1').sayHi('hi'); // 'hi' will be passed to the jQuery method 'sayHy'
$.fn.sayHi = function(txt, clr){
this.css({color: clr}).text(txt); // 'this' is your element delegated to your plugin
};
$('#hi1').click(function(){
$(this).sayHi('Heading is clicked!', 'red'); // (txt, crl)
});