Perform an action after the change has been made

Asked

Viewed 5,780 times

1

I do not know if there is such a possibility, but I would like to know if it has to execute a certain action after the action of change of a select.

Let’s assume I have one select thus:

<select name="meuSelect" id="meuSelect">
    <option value="0">Selecione</option>
    <option value="1">Primeiro item</option>
    <option value="2">Segundo item</option>
</select>

and with jQuery for me to perform something when the user selects an item I get like this:

$(document).ready(function() {
    $("#meuSelect").change(function() {
        // executa uma ação qualquer
        console.log('executou tudo por exemplo');
    });

    //somente executar a proxima ação após ter concluido tudo do 'change'
    //pensando que essa ação do change esta em outro arquivo 
    //e esta sendo chamado por um $(seletor).on('change', function(){});
    //coloquei aqui somente como exemplo, mas o change aqui não existe

    console.log('Agora sim');
});

I hope I was clear in the comments, explaining a little better my problem

  • Marcelo, I noticed from the comments of the answer given by João Paulo that what you are doing is an asynchronous request. In this type of request your callback code must be right after the request to the server or it will not work properly.

  • The next post might help you: http://answall.com/questions/2232/howto perform a function

2 answers

2

Create another change function on the page after referencing this external script.

See how it works: http://jsfiddle.net/johnflaster/Tavb5/1/

Example:

<script src="script_externo.js">

<script>

$(document).on("change", "#meuSelect", function(){
    console.log('Isso sera executado depois do primeiro change, por causa da ordem em que foi colocado na pagina');
});

</script>
  • So I did it but it didn’t work because I load this html with an ajax and with that comes this js that runs what I said, I know it’s not the best way, but it’s one that I got, but I ended up crashing there

  • Exchange $("#meuSelect"). change(Function(){ for $(Document). on("change", "#meuSelect", Function(){. That then the change will work for elements created dynamically.

  • I had already tested as well and nothing... I think I will have to arrange the calls so that it really works in the right way. Still thank you.

  • When you go to the browser console (F12) it shows some javascript error? You can paste here?

  • If you can post the real code with the external code call and with your js it also helps. Suddenly there is some silly error in the middle.

  • It doesn’t present any error. I did some tests and nothing. As I commented, it may be a mistake of my creation, so I hope that with this I have to work a little better on it.

Show 1 more comment

1

It’s just encapsulating in a function, just to keep the organization going, and then calling Function inside your callback:

$(document).ready(function() {

    function doAfterChange(){
        console.log('Agora sim');
    }

    $("#meuSelect").change(function() {
        // executa uma ação qualquer
        console.log('executou tudo por exemplo');

        // Lá vai:
        doAfterChange();
    });
});
  • I understand what you’ve been through, but I won’t be able to change the external function that makes the change, but still thank you

  • Ah, now I understand what you wrote up there :) ^^

Browser other questions tagged

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