Problem when clicking the button, call ajax

Asked

Viewed 249 times

0

Code:

<button id="click" class="button"></button>
    <div class="TableCSS">
        <table id="clickvent">
        </table>
    </div>

 

   $(document).ready(function(){
            $("#click").click(function(){
                if($("#clickvent").is(":visible")){
                        $("#clickvent").hide();
                    } else{
                        $("#clickvent").show();
                    }                   
                $.ajax({
                    url: "../Conteudo/click.html",
                    cache: false,
                    dataType: 'html'
                })
                .done(function(retorno) {
                    $("#clickvent").html(retorno);
                })
                .fail(function() {
                    alert("Algo está errado");
                });
            });
        });

It’s calling all right but when I click the button you need to double click to load the content of the page click.html. Can anyone tell me the error ? And only by pressing for the first time happens that you have to click twice, to hide the content is normal just to load that happens.

  • are you sure that’s the problem? Because apparently you hide/show the #clickvent when you click.... comments this if if($("#clickvent").is(":visible")) and see if it solves the problem...

  • I commented but there does nothing. Not an action when you click the button.

1 answer

1


You have to click 2 times to appear because when the page is loaded the condition if($("#clickvent").is(":visible")) already gives true then it executes the hide() for only the next click appear. Suggested solution is before the execution of the click from the button add the following line:

$("#clickvent").hide();

Another solution would be to add CSS the following:

#clickvent {
   display: none;
}
  • And when I have 2 buttons, how do I replace the content of the second button of the first I clicked ?

  • You want the second button to replace the table content with another?

  • That’s right, I would have ?

  • There you go @Kevin. F!

  • I asked a question if you know how to answer there http://answall.com/questions/123363/secondo-bot%C3%A3o-substituting-count%C3%Bado-do-primeiro/123392? noredirect=1#comment257141_123392]

Browser other questions tagged

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