0

I have written a script in Javascript using JQuery in which when I click on a button something happens (e.g. alert("hello"). What I am trying to do now is when I click again on the same button to run a different function. This is what I have now:

      $(document).ready(function(){
        $(".imgMarker").click(function() {
            var markers = new OpenLayers.Layer.Markers( "Markers" );
            map.addLayer(markers);
            markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0)));   
            });
        });

I have read that I can use the toggle() method but I can not make it work.

1
  • 1
    What should happen the second, third and fourth time (etc.) someone clicks the button? Commented Feb 25, 2015 at 13:53

3 Answers 3

1

Updated answer and example: EXAMPLE

$.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;
};


var addMarker = function(){
    /*
    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);
    markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0))); 
    */
    alert('Add Marker!');
};

var destroyMarker = function(){
    alert('Destroy Marker!');
};

$(".imgMarker").clickToggle(addMarker, destroyMarker);

Essentially we are adding a new method here called clickToggle() which takes two functions as arguments. This will toggle between the two functions you pass it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. The problem is that by unbinding it then after the sec click it keeps executing the last function. Is there something like this: $(this).bind("click");
What would you like the following clicks to do? Re-Add the marker? And continue to toggle back and forth?
Can I ask one more thing. Is it possible using this function to pass a global variable to addMarker() function?
You should be able to simply by this: $(".imgMarker").clickToggle(addMarker(yourVar), destroyMarker);
1

You have to remove this click function and then bind it to another when you click the first time. Try this:

$(document).ready(function(){
    $(".imgMarker").click(function() {
        var markers = new OpenLayers.Layer.Markers( "Markers" );
        map.addLayer(markers);
        markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0))); 
        $(this).unbind("click");
        $(this).click(otherClickFunction);
    });
});

5 Comments

So my otherClickFunction will be something like this: $(function destroyMarker(){alert("hello");}); ??
For some reason this is executed on page load.
var destroyMarker = function(){ alert('hello'); }; Then replace where Dhunt typed 'otherClickFunction' with 'destroyMarker'.
Thanks. The problem is that by unbinding it then after the sec click it keeps executing the last function. Is there something like this: $(this).bind("click");
What would you like the following clicks to do? Re-Add the marker?
1

I have written small plugin that count click on button, you can use it like so

$(document).ready(function() {
  $.fn.clickCounter = function () {
    return $(this).data('clicks', $(this).data('clicks') + 1 || 1) && $(this).data('clicks');
  };

  $(".imgMarker").click(function () {
    if ($(this).clickCounter() > 1) {
      console.log('do something');  
    }
  });
});

Example

Comments

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.