1

With some help I have written in JQUERY this:

   $('id').clickToggle(addMarker, destroyMarker);

where clickToggle is a function which calls addMarker and destroyMarker functions.

This is the clickToggle() function:

 $.fn.clickToggle = function(func1, func2) {

        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function() {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };

And addMarker() function is

var addMarker = function(){ ....}

I don't have so much xp in JQUERY. What I want to do is to pass a variable inside the addMarker() function. The way my code is written how can I do it?

3
  • 3
    OT: $.proxy(funcs[tc], this)() -> funcs[tc].call(this) Commented Feb 26, 2015 at 12:01
  • Which variable do you want to pass to addMarker? Commented Feb 26, 2015 at 12:02
  • Thanks for the answer! The variable is defined outside the clickToggle() function. Like this: var id = x; $('#'+id).clickToggle(addMarker, destroyMarker); Commented Feb 26, 2015 at 12:04

2 Answers 2

1

A demo will be very handy here:

jsfiddle demo

HTML

<div id="myid">click me</div>

Javascript/jquery

 $.fn.clickToggle = function(func1, func2) {

        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function() {
            var data = $(this).data();
            var tc = data.toggleclicked;
            $.proxy(funcs[tc], this)();
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };
//lets pass the strings "world" and "foo" to addMarker and destroyMarker 
$('#myid').clickToggle(function(){addMarker("world")}, function(){destroyMarker("foo")});

//pass a variable as a parameter to the functions    
var addMarker = function(x){ alert("addmarker "+x)}
var destroyMarker= function(x){alert("removemarker "+x)}
Sign up to request clarification or add additional context in comments.

7 Comments

I had a typo in the code example. It is fixed now :-)
Thanks but it doesn't seem to work. Nothing is alerted!
It should work, It is working on my PC (using firefox) so it should work on yours too.
Yeah. Strange. Instead of the div id I have an image id. It should have the same result.
What do you mean with image id? The code I posted is the same as in the Jsfiddle except for the comments.
|
1

If you're using a global variable, you can do it like this:

var x = 5;

$.fn.clickToggle = function(func1, func2) {

        var funcs = [func1, func2];
        this.data('toggleclicked', 0);
        this.click(function() {
            var data = $(this).data();
            var tc = data.toggleclicked;
            funcs[tc].call(this, x);
            data.toggleclicked = (tc + 1) % 2;
        });
        return this;
    };

$('id').clickToggle(addMarker, destroyMarker);

Fiddle

5 Comments

Thanks for the answer. But how do I pass then the x variable into the addMarker function()?
@dkar Here: funcs[tc].call(this, x);. The call allows you to pass parameters, which is what x is doing there after the this variable.
Using global variables is not recommended when not necessary.
@kasperTaeymans True, but sometimes you can't change your code and just have to make do.
@dkar Read about the .call() function here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.