Open javascript with Select Option

Asked

Viewed 113 times

0

I’m wanting to open Check() and open it when the select option is pressed.

<select>
<option href='javascript:;' onchange="abrirVit();" value="canto">Vitória - Praia do Canto</option>
<option href='javascript:;' onchange="abrirVelha();" value="velha">Vila Velha</option>
</select>

SCRIPT:

 <script type="text/javascript">
    $(function(){
    function abrirVit(){
        $("#backForm").css("display","block").stop().animate({"opacity":"1"},200);
        setTimeout(function(){
            $("#formContact").css("display","block").stop().animate({"opacity":"1"},200);
        },150);
    };
</script>



  <script type="text/javascript">
        $(function(){
        function abrirVelha(){
            $("#backForm").css("display","block").stop().animate({"opacity":"1"},200);
            setTimeout(function(){
                $("#formContact").css("display","block").stop().animate({"opacity":"1"},200);
            },150);
        };
    </script>
  • The functions abrirVit and abrirVelha are the same? what is the difference between them?

  • 1

    Remove the $(function(){ ... } that is around the functions. Not only will this solve the problem but the syntax error that has in your code (should be appearing in the browser console, open to see).

  • @Sergio is just for testing, but they will do different things.

  • @bfavaretto I’ll try now

  • @bfavaretto pulled Function {} and still not working.

  • Also took the }; of the end?

  • I only let Function open(){ ... }

  • Your jquery is declared in the header?

  • <script type="text/javascript" src="js/jquery-2.0.3.min. js"></script>

  • Ah, I hadn’t noticed, but the onchange belongs to select, not to options. You call a function from there and decide what to do based on the current value of select.

  • @bfavaretto could answer with an example of how to do please? because I do not know how to call the options for value

  • 1

    https://jsfiddle.net/62f6b9tz/

  • @bfavaretto blz.. could you explain this change?? , now how do I make it so that if A is selected it does my function?

Show 8 more comments

1 answer

1


GOT IT TO WORK.

 <select id="opcoes">
                              <option value="selecione">Selecione uma unidade</option>
                              <option value="canto">Vitória - Praia do Canto</option>
                              <option value="velha">Vila Velha</option>
    </select>

script

  $('#opcoes').on('change', mudou);

    function mudou() {
        if ($('#opcoes').val() == 'canto'){
                 $("#backForm").css("display","block").stop().animate({"opacity":"1"},200);
        setTimeout(function(){
            $("#formContact").css("display","block").stop().animate({"opacity":"1"},200);
        },150);
        }
        else if ($('#opcoes').val() == 'velha'){
                $("#backForm").css("display","block").stop().animate({"opacity":"1"},200);
        setTimeout(function(){
            $("#formContact").css("display","block").stop().animate({"opacity":"1"},200);
        },150);
        }
    }
  • 1

    That’s right. But you can do it in one if, without: if( $('#opcoes').val() == 'canto' || $('#opcoes').val() == 'velha' ) { .... Since what you do in there is the same thing for both cases.

  • so that was just an example , because the real one will get more and they will do different things ;D, but thanks friend helped a lot. @bfavaretto

Browser other questions tagged

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