-2

I tried to search for this online but couldn't find anything that works

i want that after i press then class where id = 1 is changed to cls-checked

<a id='1' class='cls-up' onclick='vote("up",this);'></a>
<a id='1' class='cls-down' onclick='vote("up",this);'></a>

after pressing vote it goes to php and like is pressed i want to make users already see like without reloading

function vote(vote, val)
        {
            var params = {"vote": vote, "id": val.id};
            $.ajax({
                data:  params,
                url:   'vote.php',
                type:  'post',
                success:  function (response) {

                }
            });
        }

problem is i always have 2 elements with same id if each element had different id i could use this code

var x = document.getElementById("1");
x.className = "cls-2";

1 Answers1

0

On click check the id and then add class to existing classList

function vote(e, status, elem) {
  e.preventDefault();
  if (elem.id === '1') {
    elem.classList.add('cls-2')
  }
}
.cls-2 {
  color: green;
}
<a id='1' class='cls' onclick='vote(event,"up",this);'>Hello</a>
brk
  • 48,835
  • 10
  • 56
  • 78