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 foralert(evento.type);
.– ptkato
@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".– Sergio
@Patrick, while you’re at it, Google
console.log()
, much better than thealert()
and you can see objects, which Alert doesn’t.– Sergio
@Patrick, you can put the code you are using in jsFiddle: http://jsfiddle.net/XBDrh/1/ for me to see
– Sergio
@Patrick the error is correct, can explain better what you want to do? because when you click on
seletor2
this parameteranimais
does not exist because thetrigger
is only fired when you pressseletor1
– Sergio
So my intention was to say, "if the user clicks directly on
seletor2
, go to thedefault
and do X, but if he clicksseletor1
, go to'gato'
and do Y. As far as I’m concerned, Undefined would play for thedefault
.– ptkato
@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 ofanimais.animal
.– Sergio
@Patrick great that worked! Good idea to add to the answer, I did it now. Tomorrow I come by and clear the comments too.
– Sergio