views:

1775

answers:

7

Hello,

I have lots of jquery functions in my script but a particular one is not working, this is my function

  $('#delete').click(function() {
          var id = $(this).val();
          $.ajax({
                     type: 'post',
                     url: 'update.php',
                     data: 'action=delete&id=' + id ,
                     success: function(response) {
                             $('#response').fadeOut('500').empty().fadeIn('500').append(response);
                             $(this).parent('tr').slideUp('500').empty();
                        }
              });  
      });

a similar function like this is working

<!-- WORKING FUNCTION -->
$('#UpdateAll').click(function() {
      $.ajax({
            type: 'post',
            url: 'update.php',
            data: 'action=updateAll',

            success: function(response) {
       $('#response').fadeOut('500').empty().fadeIn('500').append(response);

       $('#table').slideUp('1000').load('data.php #table', function() {
        $(this).hide().appendTo('#divContainer').slideDown('1000');
       });
            }
      });  
     });

I checked with firebug, the console doesnt show any errors, i checked html source the values are loading correct, i checked my php file 5times it is correct can't figure out the problem. Please Help.

+2  A: 

With the first one, I'd put a quick and nasty alert() within the click anonymous function, to ensure that it is being fired. Eliminate reasons why it may not be working. Also, try using Live HTTP headers or Firebug's console to see if the AJAX request is being sent.

If the click is not being fired, see if you have the selector correct. I often do this (quite nasty)

var testSelector = 'p:first ul li:last';

$(testSelector).css( { border: '1px solid red' } );

It won't always be visible, but if you see style="border: 1px solid red"` in the generated markup, you know your selector is on the ball.

Perhaps you have another click that is overwriting it? Try using

$('#delete').bind('click', function() {
  // do it
});
alex
i added the alert but it is not getting fired but the alert is working fine with other functions.
Shishant
A: 

1) just before the last }); you should add return false;

2) Are you sure that #delete exists? Also, are you sure is UNIQUE?

Ionut Staicu
Yes the button exists <button id="delete" value="<?=$results[$k]['id'];?>">x</button> and it is unique id name.
Shishant
I'd like to second the UNIQUE part. If the id, "delete" exists more than once in the DOM, you'll get unexpected results.
bic72
i tried placing a single button with same above code removing the html part with id of button=delete still it is not getting fired.
Shishant
inspect the DOM with Firebug, can you see the element? Does the value look kosher?
bic72
A: 

In Firebug, script tab, options menu, "Break on all errors" should be turned on. Sometimes, that will reveal the problem.

bic72
I enabled break on all errors but it is working same
Shishant
take ajax out of the equation and see if you can just trigger a click event. Reduce your code to a simple case.
bic72
A: 

This is a long shot, but it's good to be aware of. http://code.google.com/p/fbug/issues/detail?id=1948

May not be an issue if you're not on Firefox 3.5.

bic72
The example page created by him is working fine without any errors.
Shishant
Maybe take Firebug out of the equation. Even if the above bug doesn't apply, I've run into issues where Firebug has caused odd behavior in Firefox, at least temporarily. Restart Firefox.
bic72
A: 

Are you sure your page validate at w3c, have you got duplicate IDs or similar DOM issues? If it does why not upload the page, one of us will be able to diagnose the issues with the full source.

redsquare
why the -1:) I'm intrigued
redsquare
A: 

Instead of id=delete i changed it to class=delete in html and in js ('.delete') and it is working fine now but when again tried with id it doesnt work.

Thank You all for Help, i dont have any problem whether it is id or class just the function works now.

Shishant
Watch you remember about .delete, so it won't bite you down the track. It may be worth making the selector more specific, or the class 'delete-record'. Also, it'd be worth tracking down exactly why it doesn't work, in case it rears it's head again down the track.
alex
I tried everything possible and i still dont know why it didnt worked with id=delete and worked with class=delete and i also tried renaming the ids to extremely unique names but nothing helped except class=delete
Shishant
I was having the same problem and using class solved it for me too. But it was truly a class vs id problem for me. I was assigning a id to a div that was getting repeated multiple times. So duplicate id! BUT since I had this in a for loop of a mako template I overlooked the fact that it will be repeated multiple times, DOH!!! Lesson: Open the file in a browser and view the source, then see if the div element is repeated or not.
Shao
+1  A: 

I don't know if this applies in your context, but if you have parts of the page that are getting loaded by AJAX then you'll need to bind the click handlers after that content is loaded, meaning a $(document).ready isn't going to work. I've run into this problem a number of times, where certain events will fire fine until parts of the page are reloaded, then all the sudden the events seem to stop firing.

toluju