Rodrigo Dias
Rodrigo Dias

Reputation: 177

jQuery function not started

I have a weird problem. I have a freight calculation field to be executed if the User deletes a digit input , I hide one content. While running the script in chrome console, it is loaded, but when using the call in html, js it does not run. This is what I have.

https://jsfiddle.net/diasbass/u3xr0921/

jQuery(document).ready(function($) {
    $("#btnFreteSimulacao").click(function() {
        $("#txtCep").keyup(function() {
            if ($("#txtCep").val()) {
                $('p.montagem').hide();
            } else {
                $('p.montagem').show();
            }
        });
    });
});

Upvotes: 0

Views: 60

Answers (2)

brk
brk

Reputation: 50291

The keyup event handler is inside the click function, probably it is of no use.

Also need to check $("#txtCep").val().length for showing and hiding the p.montagem

jQuery(document).ready(function($) {

            $("#txtCep").keyup(function() {         
                if($("#txtCep").val().length ==0) {
                    $('p.montagem').hide();             
                } else {
                    $('p.montagem').show();
                }
            });     

    });

jsfiddle

Upvotes: 1

Don
Don

Reputation: 1334

Here you are mixing two asynchronous events 1) Button click 2) Input keyup. Your code expects to work when both are happening same time. I would suggest remove dependency on one event. Like below.

    $( "#btnFreteSimulacao" ).click(function() {                
       // $("#txtCep").keyup(function() {         
            if($("#txtCep").val()) {
                $('p.montagem').hide();             
            } else {
                $('p.montagem').show();
            }
        });     
   // });
    });

If thats not possible, try to look towards promises.

Upvotes: 1

Related Questions