0
var widescreen = {
  "flex": "0 100%",
  "width": "100%",
  "-webkit-flex": "0 100%",
  "-moz-flex": "0 100%",
  "-ms-flex": "0 100%",  
};

jQuery.fn.MonPlugin=function() {
jQuery(".size-60").css(widescreen)
};

jQuery(".titre1").bind("click",MonPlugin);

I would like to use MonPlugin as function that I include on different events but console return Uncaught ReferenceError: MonPlugin is not defined.

4 Answers 4

3

Call

jQuery(".titre1").bind("click",$.fn.MonPlugin);

instead of

jQuery(".titre1").bind("click",MonPlugin);
Sign up to request clarification or add additional context in comments.

Comments

1

Create function using this way instead :

var MonPlugin = function() {
  jQuery(".size-60").css( widescreen )
};

or

function MonPlugin() {
  jQuery(".size-60").css( widescreen )
}

Comments

0

var widescreen = {
  "flex": "0 100%",
  "width": "100%",
  "-webkit-flex": "0 100%",
  "-moz-flex": "0 100%",
  "-ms-flex": "0 100%",
};

// a plugin declared like this is added to jQueryies prototype allowing
// you to add the method to the chain of functions. but you would need to modify
// your code just a tad.

jQuery.fn.MonPlugin = function() {
  // plugins have the collection of the selected elements so loop through them all
  this.each(function(){
    // create a new jQuery object with the current element and bind your function
    jQuery(this).on('click', MonPluginCallBack );
};

// to call your plugin you would use
jQuery('.titre1').MonPlugin();


// it seems that you just want a callback function so you should use

function MonPluginCallBack( event ){
  jQuery(".size-60").css(widescreen);
}

// here you are passing the function by reference
jQuery(".titre1").bind("click", MonPluginCallBack );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2 Comments

Thanks, if i want to reset the css (normal value is 60%), which method you recommend ? Thanks
if you have access to the css file, I would do all the css there and simply toggle the class with javascript to trigger the different states. if you don't have access to the css you should save a variable in the parent state
0

custom jquery function or jquery function extenders can only be used with a jquery object. So in order to invoke a custom function, you need to attach it to an element.

$.fn.MonPlugin = function() 
{
    alert();
}

$(".titre1").bind("click",function()
{
  $(this).MonPlugin();
})

Example : http://jsfiddle.net/DinoMyte/bvtngh57/149/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.