views:

32

answers:

4

This is the function

function deleteItem(id) {
    $.post("_asyncpage.php",
        {id:id},
        function (result) {
            if (result == true) {
                $('#'+id).remove();
            }
        }, "json"); 
}

So, to explain, the function receive an id, send to a page that execute random stuff on a db and return true/false.

The function inside check for result that can be true/false as said before. If true proceed to remove the dom element that match the id passed.

The db is updated correctly but the .remove() won't work... someone knows why? :(

The following is an example of the html structure. the table inside the TD is the one to be deleted.

<td width="120" valign="top" id="13_02">
   <table cellspacing="0" cellpadding="0" class="tableProg" id="1">
   <tbody>
      <tr>
         <td colspan="3"><h4 style="margin: 0pt;">Title</h4></td>
      </tr>
      <tr>
         <td colspan="3">h. 13:35</td></tr><tr><td width="74"><span style="font-weight: bold; color: rgb(0, 102, 204);">Su</span>: TV</td>
         <td width="22"><a href="javascript:openEditItem('2010/08/24','1')"><img src="static/images/edit.gif"></a></td>
         <td width="22"><a href="javascript:deleteItem('1')"><img src="static/images/delete.gif"></a></td>
      </tr>
   </tbody>
   </table>
</td>
A: 

I would inspect your result. Do you return true as number (1)? or is it a string when received?

Since you're dataType is json, the value of result must be a json string or an already parsed object, so I bet pretty much that this

if (result == true) {

will never pass.

jAndy
I'll pass true/false as a string (output in page from the asyncpage.php) but i supposed that the operator "==" would pass for every value equal to false as 0, '', null, etc.Anyway i'll try with:if (result == 'true')Thanks for the celerity of the suggest :)
theCrius
Oki, that was not the problem.Using a simple alert i've checked that the process enter the if but simply not execute the jquery command.
theCrius
A: 

Is the $('#'+id).remove(); getting executed?

What happens if you do alert($('#'+id).length)? You should expect an answer > 0

Also, what is the type of result? Try alert(typeof(result)); This will help you determine the correct comparison for your if check

James Wiseman
Oki, the result is 'boolean'.The lenght of the id is 1 at the moment.id will be always a number (corrispond to the id of the record into the database) and it's the id of a table created inside a table.
theCrius
+1  A: 
patrick dw
very unlikely that `result` contains a string, if it's a `json string` like it should be.
jAndy
The function $.post has been called with the attribute ("json") that means it parse the result as a jsonObject so true/false has been automatically converted into boolean from string. I'm saying crap stuff?
theCrius
@theCrius - Good point. Very true.
patrick dw
While i'll try another solution i'm agree with you. I wish to have more reputation to give you a vote :(Hope my gratitude will be enough :)
theCrius
@theCrius - That's plenty. :o)
patrick dw
@theCrius - By the way, what @James Wiseman was saying was to test the `length` property of the jQuery object. This will tell you how many matches were found. If `$('#'+id).length` gives you `0`, then you know there was no match.
patrick dw
Yep, i had understand it and that was very smart ;)i'm *saved* this procedure for the next time i got problem with js :)
theCrius
A: 

Just to give more info, this is a portion of the structure that need to be removed via jquery.

<td width="120" valign="top" id="13_02">
   <table cellspacing="0" cellpadding="0" class="tableProg" id="1">
   <tbody>
      <tr>
         <td colspan="3"><h4 style="margin: 0pt;">Title</h4></td>
      </tr>
      <tr>
         <td colspan="3">h. 13:35</td></tr><tr><td width="74"><span style="font-weight: bold; color: rgb(0, 102, 204);">Su</span>: TV</td>
         <td width="22"><a href="javascript:openEditItem('2010/08/24','1')"><img src="static/images/edit.gif"></a></td>
         <td width="22"><a href="javascript:deleteItem('1')"><img src="static/images/delete.gif"></a></td>
      </tr>
   </tbody>
   </table>
</td>
theCrius
Please post additional information in your question instead of in an answer. I'll transfer this information. Please see the update in my answer.
patrick dw
sorry, forgot the 'edit' function -.-"going to do it.
theCrius