Prva pomoć javascript

imam 3 dugmica

<div class="like" onclick="like()"><span class="brojac1">4</span></div>
<div class="liked"><span class="brojac2">5</span></div>
<br>

<div class="like" onclick="like()"><span class="brojac1">4</span></div>
<div class="liked"><span class="brojac2">5</span></div>
<br>

<div class="like" onclick="like()"><span class="brojac1">4</span></div>
<div class="liked"><span class="brojac2">5</span></div>

kako napraviti da se individualno lajka svaki dugmic?

function like(){
  $.ajax({
    type: "GET",
    url: "like.php",
    data: { like_id: '1' },
    success:function(data){
console.log(data);
$(".liked").show(),
$(".like").hide()
}
  });
return false; // Prevent the browser from navigating to the-script.php
};

Ovako “iz rukava”:

<div class="like" onclick="like(1)" id="like1"><span class="brojac1">4</span></div>
    <div class="liked" id="liked1"><span class="brojac2">5</span></div>
    <br>
    
    <div class="like" onclick="like(2)" id="like2"><span class="brojac1">4</span></div>
    <div class="liked" id="liked2"><span class="brojac2">5</span></div>
    <br>
    
    <div class="like" onclick="like(3)" id="like3"><span class="brojac1">4</span></div>
    <div class="liked" id="liked3"><span class="brojac2">5</span></div>



    function like(id){
      $.ajax({
        type: "GET",
        url: "like.php",
        data: { like_id: id },
        success:function(data){
    console.log(data);
    $("#liked"+id).show(),
    $("#like"+id).hide()
    }
      });
    return false; // Prevent the browser from navigating to the-script.php
    };
1 Like

Zahvaljujem :smiley:

1 Like

No prob. Da se to izvest i elegantnije, al ovo je onako, “nabrzaka” :smile:


Copyright © 2020 WM Forum - AboutContact - Sponsored by: Mydataknox & Profit Monkey