Send data along with event

Asked

Viewed 75 times

6

As I venture through jQuery some questions and problems arise in the middle of the way, one of them is: is it possible to send a data through an event triggered by another event? Something like that:

jQuery

$(".seletor1").on("click", function(){
    $(".seletor2").trigger("click", dados);
});
//
$(".seletor2").on("click", function(dados){
    alert(dados);
});

1 answer

6


Yes you can. Example: http://jsfiddle.net/XBDrh/

$(".seletor1").on("click", function(){
    console.log('Clicado!');
    $(".seletor2").trigger("click", {animal: 'gato'});
});
//
$(".seletor2").on("click", function(evento, animais){
    alert(evento.type);
    alert(animais.animal);
});

Looking at jQuery documentation the expected syntax is:

.Trigger( typeEvent [, parametrosExtra] )

windy type
Typo: String
Description: A string satisfies the name of the event. For example click or Submit'

parametrosExtra
Typo: Array or Object
Description: Additional parameters to pass with the event.


If you want to mix this Handler click with a real click you can create one if to check if the extra object has been passed. Suggestion:

$(".seletor2").on("click", function (evento, animais) {

    if (!animais) {
        alert("Clicou no seletor2");
        return false;
    }

    switch (animais.animal) {
        case 'gato':
            alert("Clicou no seletor1");
            break;
        case 'cao':
            alert("Clicou no seletor1");
            break;
        case 'elefante':
            alert("Clicou no seletor1");
            break;
    }
});
  • In the case animal it’s like an address to 'gato'? I just don’t understand the need for alert(evento.type);.

  • 1

    @Patrick, event is an object. I put evento.type because that property is by default the name of the event, and it’s just to confirm that it’s the right name. The second parameter is also an object whose key is "animal" and the value is "cat".

  • @Patrick, while you’re at it, Google console.log(), much better than the alert() and you can see objects, which Alert doesn’t.

  • @Patrick, you can put the code you are using in jsFiddle: http://jsfiddle.net/XBDrh/1/ for me to see

  • @Patrick the error is correct, can explain better what you want to do? because when you click on seletor2 this parameter animais does not exist because the trigger is only fired when you press seletor1

  • So my intention was to say, "if the user clicks directly on seletor2, go to the default and do X, but if he clicks seletor1, go to 'gato' and do Y. As far as I’m concerned, Undefined would play for the default.

  • 1

    @Patrick, okay, so I understand better what you want. How many options will you have? if there are many I would suggest something like this: http://jsfiddle.net/7euUL/1/ (Anyway the problem is that the animais is not closed in case of click on selector 2, and so it gives error when you want to know the value of animais.animal.

  • @Patrick great that worked! Good idea to add to the answer, I did it now. Tomorrow I come by and clear the comments too.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.