-3
My code is very repetitive, I want to use a loop to solve this, but I don’t know how to do it.
$(document).ready(function()
{
//Atribui aos campos de código do Pokemon a função que carrega os dados dos pokemons:
$("#pok11").focusout(function(){
carregarPokemon("pok11");
});
$("#pok12").focusout(function(){
carregarPokemon("pok12");
});
$("#pok13").focusout(function(){
carregarPokemon("pok13");
});
$("#pok14").focusout(function(){
carregarPokemon("pok14");
});
$("#pok15").focusout(function(){
carregarPokemon("pok15");
});
$("#pok16").focusout(function(){
carregarPokemon("pok16");
});
$("#pok21").focusout(function(){
carregarPokemon("pok21");
});
$("#pok22").focusout(function(){
carregarPokemon("pok22");
});
$("#pok23").focusout(function(){
carregarPokemon("pok23");
});
$("#pok24").focusout(function(){
carregarPokemon("pok24");
});
$("#pok25").focusout(function(){
carregarPokemon("pok25");
});
$("#pok26").focusout(function(){
carregarPokemon("pok26");
});
```
Could insert the
Html
concerning a doubt tbm?– LeAndrade
As you did not inform the HTML, follow a "kick": https://jsfiddle.net/eckqrxoy/3/ - the idea is to use Event delegation, as already said in a comment below: instead of having several
focusout
, put only one in the parent element, which will already serve the children as well - of course it depends on how the HTML is, but as it was not informed, is the suggestion– hkotsubo
@Augustovasques As it was just to give a general idea (and not a solution - otherwise I would have answered), nor did I care for these details. But yes, depending on the user’s actions, you can loop in (and I’m too lazy to tidy up) :-)
– hkotsubo
If any answer solved your problem and there was no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you prefer one of the other answers, you can mark it as correct/accepted, but only one answer can be marked that way. If you still have any questions or would like further clarification, feel free to comment. Also see How to say thank you in replies?
– Augusto Vasques
Regarding your new question. Create a new question by pressing the button Ask a question located in the upper right corner of the page. To learn more about the operation of the site see the Community FAQ.
– Augusto Vasques