How to change passed variable inside event handler

Bartłomiej Sobieszek

I would like to know if there is a way to change "external" variable with handling the event

function generateHTML()
{
    var html = '';

    // Some html generating code
    html += '<b>John Doe</b>';

    $(document).trigger('html:generating', html);

    // Do more hardcoded stuff with html

    $(document).trigger('html:generated', html);

    return html;
}

$(document).on('html:generating', function(e, html) {
    html = '<span>' + html + '</span>';
});

$(document).on('html:generated', function(e, html) {
    console.log(html);  // prints <b>John Doe</b>
                        // expected <span><b>John Doe</b></span>
});

generateHTML();

This would be very useful when handling Mustache (or any other template library) dynamic html code.

Kamyar Infinity

If you are talking about passing html by reference, there is a way to do it. Everything you pass as a function argument is passed by value, but if you pass an object, the object properties still point to the original values, as the pointers are copied. So, you could do something like this:

function generateHTML()
{
    var container={html: ''};

    // Some html generating code
    container.html += '<b>John Doe</b>';

    $(document).trigger('html:generating', container);

    // Do more hardcoded stuff with html

    $(document).trigger('html:generated', container);

    return container.html;
}

$(document).on('html:generating', function(e, container) {
    container.html = '<span>' + container.html + '</span>';
});

$(document).on('html:generated', function(e, container) {
    console.log(container.html);
});

generateHTML();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use variable as constant inside event handler

From Dev

How to change a variable from an activity inside my adapter (java class change variable passed to it)

From Dev

How to create click event handler inside mouseover event handler?

From Dev

How to free an object inside its event handler?

From Dev

how to access to "$(this)" object from event handler passed to "on" method?

From Dev

Jquery prop function doesn't work inside Change event handler

From Dev

Event handler inside event handler is multiplying

From Dev

error this.style is undefined inside an event handler passed ona react compopnent

From Dev

How to differentiate event handler for handling checkbox change event

From Dev

Change selector in event handler

From Dev

On property change event handler

From Dev

How to change a variable inside subroutine?

From Dev

How do I unhook an lambda event handler inside the lambda method?

From Dev

How safe is to access this.state inside an event handler?

From Dev

How safe is to access this.state inside an event handler?

From Dev

How do I change a variable inside a variable?

From Dev

Return inside event completed handler

From Dev

Enaml Get Arguments Passed in an Event Handler

From Dev

Event handler passed as argument to a method does not unregister

From Dev

How do I change the name of an existing event handler?

From Dev

How to change Tabs using JavaFX by outside event handler?

From Dev

How do I change the name of an existing event handler?

From Dev

How to change value of passed Lambda expression inside method

From Dev

How to pass a template variable to event handler method in Vue js

From Dev

How can I use a variable outside of event handler?

From Dev

Change event fires jquery handler but not javascript handler

From Dev

How do I get the element that an event handler was attached to from inside the handler?

From Dev

How to mutate global variable passed to and mutated inside function?

From Dev

Knockout change event-handler

Related Related

  1. 1

    Use variable as constant inside event handler

  2. 2

    How to change a variable from an activity inside my adapter (java class change variable passed to it)

  3. 3

    How to create click event handler inside mouseover event handler?

  4. 4

    How to free an object inside its event handler?

  5. 5

    how to access to "$(this)" object from event handler passed to "on" method?

  6. 6

    Jquery prop function doesn't work inside Change event handler

  7. 7

    Event handler inside event handler is multiplying

  8. 8

    error this.style is undefined inside an event handler passed ona react compopnent

  9. 9

    How to differentiate event handler for handling checkbox change event

  10. 10

    Change selector in event handler

  11. 11

    On property change event handler

  12. 12

    How to change a variable inside subroutine?

  13. 13

    How do I unhook an lambda event handler inside the lambda method?

  14. 14

    How safe is to access this.state inside an event handler?

  15. 15

    How safe is to access this.state inside an event handler?

  16. 16

    How do I change a variable inside a variable?

  17. 17

    Return inside event completed handler

  18. 18

    Enaml Get Arguments Passed in an Event Handler

  19. 19

    Event handler passed as argument to a method does not unregister

  20. 20

    How do I change the name of an existing event handler?

  21. 21

    How to change Tabs using JavaFX by outside event handler?

  22. 22

    How do I change the name of an existing event handler?

  23. 23

    How to change value of passed Lambda expression inside method

  24. 24

    How to pass a template variable to event handler method in Vue js

  25. 25

    How can I use a variable outside of event handler?

  26. 26

    Change event fires jquery handler but not javascript handler

  27. 27

    How do I get the element that an event handler was attached to from inside the handler?

  28. 28

    How to mutate global variable passed to and mutated inside function?

  29. 29

    Knockout change event-handler

HotTag

Archive