how could I use instead of passing an id, passing a Class in JQUERY?

Asked

Viewed 95 times

1

I have this code here: HTML: <button class="btn btn-danger btn-circle delete" type="button"><i class="fa fa-remove"></i></b</td>

$('#submit1, #submit2').click(function () {
   if (this.id == 'submit1') {
      alert('Submit 1 clicked');
   }
   else if (this.id == 'submit2') {
      alert('Submit 2 clicked');
   }
});

I’d like to pass one class in place of a id, how do I? Should I use a this.class in place of this.id? and pass :

$('.submit1, .submit2').click(function () {
       if (this.class == 'submit1') {
          alert('Submit 1 clicked');
       }
});
  • Include your html to the question

3 answers

1


If I understand correctly the question you seek something like this.

$('.btnSubmit').click(function () {      
    var button = $(this);
    //console.log("Você clicou em: " + $(this).attr('id'));
    if($(button).hasClass('sim'))
    {
      swal({
        title: 'Obrigado por escolher SIM!',
        html: '<strong>Informe seu e-mail:</strong>',
        input: 'text',
        preConfirm: (value) => {
          if (!value) {
            swal.showValidationError('Não pode ser vazio!')
          }
        }
      }).then((result) => {
        console.log(result)
      });
    }
    else
    {
      swal({
        title: 'Você escolheu não!',
        html: '<strong>Obrigado mesmo assim</strong>'        
      }); 
    }
});
@import "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.3.5/sweetalert2.min.css";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.3.5/sweetalert2.min.js"></script>
<button id="Submit1" class="btnSubmit sim">Sim</button>
<button id="Submit2" class="btnSubmit">Não</button>

  • yes it is more or less that. click on a button to bring me an Alert using a "Sweet Alert"

  • I changed the answer using Sweet Alert and validating the conditions.

  • Thanks @Leandro Angelo.

1

The code you have for click events is a little strange and should be:

$('#submit1').click(function () {
   alert('Submit 1 clicked');
});

$('#submit2').click(function () {
   alert('Submit 2 clicked');
});

If you need to get to the element through the class the only thing you need to change is the selector, going from # for .

$('.submit1').click(function () {
   alert('Submit 1 clicked');
});

$('.submit2').click(function () {
   alert('Submit 2 clicked');
});

This will imply that you have the class assigned to the correct element in html.

Edit:

If you wanted to keep the style you had (which I do not advice) you could do it based on hasClass:

$('.submit1, .submit2').click(function () {
     if ($(this).hasClass('submit1')) {
        alert('Submit 1 clicked');
     }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" class="submit1" value="Submit 1">
<input type="submit" class="submit2" value="Submit 2">

  • That way I did it already and it worked but I wanted to know the way I specified, if there was a way to do it. But your answer is good.

  • @Gladiator The way he had to use $(this) to fetch the Jquery object and would have to query if you have the class as method hasClass, but ends up reinventing the wheel since the selectors serve precisely to get to the elements you want.

  • Got it. thanks for the tip @Isac

0

You can use this butter:

$('.submit1, .submit2').click(function () {
    if ($(this).attr('class').indexOf('submit1') >= 0) {
        alert('Submit 1 clicked');
    }
    else if ($(this).attr('class').indexOf('submit2') >= 0) {
        alert('Submit 2 clicked');
    }
}
  • didn’t work out @rLinhares

Browser other questions tagged

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