Get the parameters of the onclick parent event

1

I have a function that is being called in a click event and has a $container parameter defined

I'm trying to stop the process of that function, open a modal that has a save button and then continue with the original function by passing the click event to the $container parameter.

I have the following code but it does not work at all well, I explain them better in the code comment.

gm.addEditableAreaClick = function (container) {
        $("#myModal").modal();
        console.log(container); 
//aquí el container cambia cada vez que hago click

        $('.save-module').on("click", {param: container}, function(event){
            console.log(event.data.param); 
//aquí ya no cambia sino que se mantiene el que he clickado la primera vez y lo que me interesa es que cambie.

            $(('.' + gm.options.gmToolClass + ':last'), event.data.param)
                .before(elem = $('<div>').addClass(gm.options.gmEditRegion + ' ' + gm.options.contentDraggableClass)
                .append(gm.options.controlContentElem + '<div class="' + gm.options.gmContentRegion + '">[moz_module]module-id[/moz_module]</div>')).before(cTagClose).prev().before(cTagOpen);
            gm.initNewContentElem(elem);
        });
    };
    
asked by Infobuscador 14.04.2016 в 22:42
source

2 answers

2

It happens because there is a closure , and the value of container is locked in the function.

There are different solutions, the simplest one is to remove the existing driver, before establishing the new on click handler.

$('.save-module').prop('onclick',null).off('click'); 
$('.save-module').on('click', // resto del código.

Then you re-establish it and you will have the new container assigned.

    
answered by 14.04.2016 / 23:18
source
0

Declaring container you can use it in the whole scope of the function

gm.addEditableAreaClick = function (container) {
    var _container = container; // ** DECLARA CONTAINER
                $("#myModal").modal();
                console.log(container); 
        //aquí el container cambia cada vez que hago click

                $('.save-module').on("click", {param: container}, function(event){
                    console.log(_container);  // ** USAR CONTAINER
        //aquí ya no cambia sino que se mantiene el que he clickado la primera vez y lo que me interesa es que cambie.

                    $(('.' + gm.options.gmToolClass + ':last'), event.data.param)
                        .before(elem = $('<div>').addClass(gm.options.gmEditRegion + ' ' + gm.options.contentDraggableClass)
                        .append(gm.options.controlContentElem + '<div class="' + gm.options.gmContentRegion + '">[moz_module]module-id[/moz_module]</div>')).before(cTagClose).prev().before(cTagOpen);
                    gm.initNewContentElem(elem);
                });
            };

It should work, try it

    
answered by 12.05.2017 в 20:30