BrenLib
BrenLib

Reputation: 13

Using Jquery with Multiple IDs

So I have a table that shows online players in my gameserver and I'm trying to create a button that slaps that player, the script uses the players ID to slap him however my jquery script will only slap the first player in the table.

Any help is very much appreciated, here is my code

function slap(){
  
    var playerid = $("#playerid").val();
  
    $.post("q3/slap.php", { playerid: playerid }, function ( data ) {
       // populate data here
    });
}
   echo "<form><input type='hidden' id='playerid' value='$playerid'><input type=button class='actionbutton' value='Slap' onClick='slap()'></form>";

Upvotes: 0

Views: 54

Answers (2)

Drudge Rajen
Drudge Rajen

Reputation: 7987

Id is used for unique identity. Use class instead of id

 echo "<form><input type='hidden' class='playerid' value='$playerid'><input type=button class='actionbutton' value='Slap' onClick='slap($playerid)'></form>";

and in your jquery

 function slap(playerid){

        $.post("q3/slap.php", { playerid: playerid }, function ( data ) {
           // populate data here
        });
    }

Upvotes: 0

JustOnUnderMillions
JustOnUnderMillions

Reputation: 3795

First id is unique in HTML so if you have more than one player the id is useless.

But try:

function slap(playerid){
        
    $.post("q3/slap.php", { playerid: playerid }, function ( data ) {
       // populate data here
    });
}
   echo "<form><input type=button class='actionbutton' value='Slap' onClick='slap($playerid)'></form>";

that should work.

Upvotes: 1

Related Questions