13
I have a function called by two different HTML elements and each element calls the function through a given event.
$("#tempoInicial").on("blur", manipulaTempoFinal);
$("#operadorTempo").on("change", manipulaTempoFinal);
function manipulaTempoFinal() {
var tempoInicial = $("#tempoInicial");
var operador = $("#operadorTempo");
if (operador.val() == "4" && tempoInicial.val() != "") {
tempoFinal.removeAttr("disabled");
$("#tempoInicial, #operadorTempo").rules("remove", "skip_or_fill_minimum");
$("#tempoInicial, #operadorTempo, #tempoFinal").rules("add", {
require_from_group: [3, ".temposOperadorWeb"],
messages: {
require_from_group: "Ao entrar com tempo inicial e escolher o operador \"Entre\" o campo tempo final passa a ser obrigatório."
}
});
} else {
tempoFinal.attr("disabled", "disabled");
tempoFinal.val("");
$("#tempoInicial, #operadorTempo, #tempoFinal").rules("remove", "require_from_group");
$("#tempoInicial, #operadorTempo").rules("add", {
skip_or_fill_minimum: [2, ".temposOperadorWeb"],
messages: {
skip_or_fill_minimum: "Ao preencher o tempo inicial selecione o operador ou vice-versa."
}
});
}
}
Is there any way in the function manipulaTempoFinal I identify who called it and/or what event?
Excellent answer! Added enough to know this information. Just one question, the way you did you add both to the change and to the selects and inputs Blur the function, correct?
– Philippe Gioseffi
@Philippegioseffi Correto!
– utluiz
@Philippegioseffi Note that I updated the response with some nuances about
event.target
andthis
.– utluiz
It improved even more even the answer, ie using this could not always solve my problem depending on how it was called. Thank you even for the knowledge!
– Philippe Gioseffi
+1, much clearer. =)
– OnoSendai