Alert function in Jquery

Asked

Viewed 7,600 times

-1

Good morning!

Can anyone tell me a way to find syntax errors easily in Jquery?

I have a simple code of "Alert" that works one way, but not another...

That’s how it works, but fucked:

<script>
$(window).click(function(){
    $('.card.Billet')
    alert("teste");
});
</script>

But Alert gets super buggy and everything that loads or clicks appears the alert.

In the second option, trying to understand how the structure of the code works, I did so, but it did not work:

<script>
$('.card.Billet').click(function(){ 
    alert("teste");
});
</script>

Html:

<label class="card Billet"><input type="radio" value="Billet" class="rdoCreditCards" name="CreditCardProvider" id="CreditCardProvider" displayname="Boleto"><span><small>Boleto</small></span></label>
  • 1

    Gabriel, to give an adequate answer we would have to see the HTML how it is, but, only by js the first code everything that clicks fires the Alert, because you linked the event click in the element window that is to click anywhere in the window will fire Alert. Already in the second code does not work pq may not be declared the classes card and Billet in Html.

  • I added the HTML in the question bro, actually it is only one element, but as in css I do Xyz.xyz.Xyz I did so, without the space, and yes with the (.)

  • Take the point that works: de: .card.Billet to: .card

1 answer

-1

Primeiro de um espaço entre as suas classes css

$('.card .Billet')

depois adicione
e.preventDefault();

vai ficar assim:

$('.card .Billet').click(function(){
    e.preventDefault();
    alert("teste");
});

  • @Daniel Pyres didn’t work bro, but the syntax then is correct?

  • That’s right, I do. changes its html <label class="card Billet"><input id="test" type="radio" value="Billet" class="rdoCreditCards" name="Creditcardprovider" id="Creditcardprovider" displayname="Boleto"><span><small>Boleto</small></span></span></label> para <label class="card Billet"><input type="radio" value="Billet" class="rdoCreditCards" name="Creditcardprovider" id="Creditcardprovider" displayname="Boleto"><span><small>Boleto</small></span></label>

  • and changes $('.card .Billet'). click(Function(){ e.preventDefault(); Alert("test"); }); $('#test'). click(Function(){ Alert("test"); });

  • if you still have doubts, please visit: https://www.w3schools.com/jquery/event_click.asp

  • Just one more thing, I noticed that you put your input, inside the label. Usually this does not influence the js in anything, but take a look at that there. I don’t know if this is inside a <form> tag, if it is error yes.

Browser other questions tagged

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