Customize jQuery function [duplicate] - javascript

This question already has answers here:
Override jQuery functions
(2 answers)
Closed 9 years ago.
Is it possible add some function to jquery functions like hide() or show()? The objective is always that I use the function hide(), it will execute jquery base and my own function.
If yes, can you show me how? Thanks.

You could override the function. Check Override jQuery functions for a more detailed answer.
Edit:
jQuery.fn.show = function() {
// Your custom code
}
But I wouldn't override jQuery's methods when you could create your own. Overriding would very likely cause unexpected behavior in plugins.

Related

How to get the self-DOM object in callback [duplicate]

This question already has answers here:
jQuery Button Click 'this'
(3 answers)
Closed 5 years ago.
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
this.addClass('btn-default'); it didn't works.
I would like to get self-dom object(in this case $("#subPanel") itself) from inside the call back.
It might be easy problem, so I try to googled around.
However I couldn't get straight answer.
could you help me ??
Inspect this and you will see it's not a jquery object but a DOM element which does not have an addClass method. Try:
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
$(this).addClass('btn-default')
})
Example: https://jsfiddle.net/14s0h3dr/

Use a variable as function in jquery [duplicate]

This question already has answers here:
Javascript dynamically invoke object method from string
(5 answers)
Closed 8 years ago.
I want to use a method in which i will pass a function_name as parameter to another function.
On the other function, the parameter will be treated as a function
here's my code example
<div class="btn_crt_acct" onclick="toggle_object('register_div','slideDown');">
CREATE AN ACCOUNT
</div>
whch will call a function like this
function toggle_object(obj,fun)
{
$('#'+obj).fun('slow');
// fun => slideDown
// so $('#'+obj).fun('slow'); => $('#'+obj).slideDown('slow');
}
but i am doing something wrong as it states an error in console, $fun(..) is not a function.
How can i make it work perfectly??
Thanks
You'd do that with bracket notation
$('#'+obj)[fun]('slow');
FIDDLE
But why not use a proper event handler, and slideToggle if you intend to toggle it
$('.btn_crt_acct').on('click', function() {
$('#register_div').slideToggle();
});

how to rebind the onclick property in javascript [duplicate]

This question already has answers here:
jQuery equivalent of JavaScript's addEventListener method
(7 answers)
Closed 8 years ago.
I have the below code where I successfully disable the properties
.prop("onclick", null)
.prop("onmouseover", null)
.prop("onmouseout", null);
Now if I want to rebind the click event what should I do.
For adding events back to a element use the bind function.
$("YourElement").bind("event",function(){
//Do what you want.
}
You can do it with many ways. I don't use to call the prop() method, but I think you just have to use it and set as your second parameter the name of your function.
function yourFunction()
{
// The job with onclick
}
DOMElement.prop("onclick", "yourFunction");
You can also use anonymous functions.
Use this documentation to work : http://api.jquery.com/prop/
I suggest you should use removeProp() instead of prop("yourprop", null) !

Are there well known patterns to use for creating method chaining in javascript? [duplicate]

This question already has answers here:
How does basic object/function chaining work in javascript?
(5 answers)
Closed 8 years ago.
What is the best way to implement method chaining in javascript?
function CONSTRUCT(){
this.func1=function(insert_ob){insert_ob.appendTo=this;/*other code*/};
this.func2=function(...){};
this.func3=function(...){};
}
function My_Object_Creator(){this.appendTo=null;}
object1=new CONSTRUCT();
my_object=new my_Object_Creator();
object1.func1(my_object).func2().func3();
I know I can adjust those functions to in the constructor to achieve it but I want to know if there are some well-known methods or patterns one should follow while creating method chaining possibility in javascript?
The most basic way to method chain is to return this (or a reference to the object).

why do we need to pass in window and undefined into this jquery plugin? [duplicate]

This question already has answers here:
What is the purpose of passing-in undefined?
(3 answers)
Closed 8 years ago.
I'm looking at jquery resize plugin and can't understand certain things about how it works:
usually we only pass in Jquery object into jquery plugins, like this:
(function($){
....plugin code....
})(jQuery);
In "resize" plugin there are window and undefined objects being passed in:
(function($,window,undefined){
....plugin code....
})(jQuery,this);
IMHO - window is a global object anyway - why do we need to pass it in? the logic behind passing in undefined object I understand even less. I'm sure there's gotta be some reason for it - but I cannot think of any.
Can someone explain why is it being done?
this is explained very well in this video.
basically, you can set those variables in the self invoking function to ensure they work as expected.
"the asshole effect" undefined = true; -paul irish
furthermore by passing these as arguments they can also be minified.
ie.
(function(A,B,C){
....plugin code....
})(jQuery,this);

Categories