PHP Ferrari
PHP Ferrari

Reputation: 15616

Hide selected TR using JQuery

I have following table where each row has a unique ID. Now I want, if I click to Remove link the row ID will pass to a PHP file t remove record from MySQL DB and then selected row will just hide with JQuery magic.

<table id="projects_rec">
  <tbody>
    <tr id="MjY=">
      <td>Dany</td>
      <td><a onClick="remove('MjY=')" class="actions">Remove</a></td>
    </tr>
    <tr id="MjU=">
      <td>King</td>
      <td><a onClick="remove('MjU=')" class="actions">Remove</a></td>
    </tr>
    <tr id="MjQ=">
      <td>Test 2</td>
      <td><a onClick="remove('MjQ=')" class="actions">Remove</a></td>
    </tr>   
  </tbody>
</table>

I wrote

function remove(mid){
    document.getElementById(mid).style.display='none';
}

but how do it pass ID to PHP file and then hide TR with SLOW effect?

Upvotes: 0

Views: 330

Answers (3)

StephenD
StephenD

Reputation: 1188

You can use jquery to hide the ID by using:

$('#'+id).hide('slow');

assuming that your javascript function remove looks similar to this.

remove(id)
{
  $.ajax({
    url: url here,
    type: "POST",
    data: {id : id },
    success: function(data)
    {
        var row_id = id.toString().replace(/=/g, "\\=");
        $('#'+row_id).hide('slow');    
    },
    error: function (xhr, textStatus, errorThrown)
    {
        alert("Error");
    }
    });
}

Upvotes: 1

Tats_innit
Tats_innit

Reputation: 34107

Working demo http://jsfiddle.net/feake/

You need to escape the character = in your id and in the sample demo I replaced it with escape \\= and Bingo it works.

This will help you! :)

code

function remove(row_id){
    var foo = row_id.toString().replace(/=/g, "\\=");

    $("#"+foo).css('display','none');

  }​

Upvotes: 0

JIA
JIA

Reputation: 1495

<tr id="MjQ=">
  <td>Test 2</td>
  <td><a onClick="remove('MjQ=',this)" class="actions">Remove</a></td>
</tr> 

in the remove function

function remove(id,$this){
 //do the php stuff
 $.post('remove.php',{id:id},function(){
   $("#"+id).hide();
 });     
}

on the php end you can get the id as

$id=$_REQUEST['id'];

Upvotes: 0

Related Questions