Add class using Jquery in a specific form?

Asked

Viewed 67 times

2

How to add a class to the palette button I click on "Like"?

For example, I have several color palettes, but when I click on Like, all buttons get the class .red, I wanted just what I clicked to get the class:

My HTML:

<form method="post" id="like">
    <div class="form-group">
        <input type="hidden" name="id" value="<?=$row->id?>" class="form-control">
    </div>
    <div class="form-group">
        <input type="hidden" name="url" value="<?=Url::urlBase('palette/ajax/like')?>" class="form-control">
    </div>
    <button type="submit" name="submit" class="btn btn-light btn-sm">
        <i class="fas fa-heart"></i> <?=$row->likes?>
    </button>
</form>

My code ajax:

$('form#like').submit(function (e) {
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: $('#like input[name=url]').val(),
        data: $(this).serialize(),
        dataType: 'json',
        success: function (response) {
            if (response.success) {
                $('#like button[name=submit]').addClass('red')
            }
        }
    });
});

And taking advantage of the topic, I would also like to know how I can keep the red class active even if it gives Reload on the page? Since I am not using a login system, here everyone who enters the site can give the Ikes. In the future I will do to excuse.

  • What’s wrong with the question?

1 answer

5


See if it helps you:

$(document).on("click", ".botao-curtir", function(e) {
  //e.preventDefault();

  $(this).addClass('red');

});
<form method="post" id="like">
  <div class="form-group">
    <input type="hidden" name="id" value="<?=$row->id?>" class="form-control">
  </div>
  <div class="form-group">
    <input type="hidden" name="url" value="<?=Url::urlBase('palette/ajax/like')?>" class="form-control">
  </div>
  <button type="submit" name="submit" class="btn btn-light btn-sm botao-curtir">
        <i class="fas fa-heart"></i> <?=$row->likes?>
    </button>
</form>

  • If it is according to your code, you can add a class or an id to your Submit button and, on the return of AJAX, add this class 'red' to the button, taking as a reference the class or ID you gave it. Behold:

$('form#like').submit(function(e) {
  e.preventDefault();

  $.ajax({
    type: 'POST',
    url: $('#like input[name=url]').val(),
    data: $(this).serialize(),
    dataType: 'json',
    success: function(response) {
      if (response.success) {
        $('.botao-like').addClass('red')
      }
    }
  });
});

  • To identify a button, you do not need to use the name attribute (Generally, the name attribute serves to represent a collection of values, sent through a form, to the server), you can use ID or Class.
  • It was the same at skoask, when click add on all buttons, in case it has several palettes, see :i.stack.Imgur.com/k2gmH.png

  • You put the boot-like class on the button ?

  • Yes, I put..

  • An error is being displayed in Devtools ?

  • None... It worked, only in the same way... all the buttons end up changing, I would like only what was clicked to change

  • Max, but you put the class on all buttons or just on what you want to change the color ?

  • Jason, like, these palettes are coming from the database, in case I used a foreach, then if I put the class on the button, they all change...

  • Jason, that little problem discussed in the chat room I was able to solve using var btn = $(this).children().last().attr('id'); with your code and this is now 100% Working oaksokas.

  • Nothing! Ball show, brothão.

Show 5 more comments

Browser other questions tagged

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