views:

34

answers:

3

Morning/Afternoon guys.

Writing some JQuery AJAX shizz and getting a bit stuck. I've got the actual proccess of calling the php file done perfectly, its just trying to get the html on the page to change in a way I want it to. I want to get rid of the a with the id of the one used in the ajax call etc, and replace it with the html passed from the PHP file. Code is as follows...

$(".save_places").click(function() {
  $.ajax({
    url: "{/literal}{$sRootPath}{literal}system/ajax/fan_bus.php",
    type: "POST",
    data: ({id : this.getAttribute('id')}),
    dataType: "html",
    success: function(msg){
      $(this).before(msg);
      $(this).empty();
      alert(msg);
    }
  });
  return false;
});

And the HTML is pretty simple;

<p class="links">
  <a href="#" class="save_places" id="bus_{$businesses.results[bus].id}_{$sMemberDetails.id}"><img src="{$sThemePath}images/save_places.png" alt="Save to My Places" /></a>
  <a href="#"><img src="{$sThemePath}images/send_friend.png" alt="Send to a Friend" /></a>
</p>

All the stuff in the success function is experimental mashing of code, any help please?

Thanks as always.

+1  A: 
linksPara.replaceChild(newElements, oldAtag);
Delan Azabani
+3  A: 

I think what you're after is .replaceWith(), like this:

$(this).replaceWith(msg);

This replaces the <a></a> with the content coming back in msg.

Also, if you're sure the elements have IDs, you can just do this:

data: {id : this.id},
Nick Craver
Nick, the refined data: stuff works a treat, much neater, thanks! Although the replaceWith doesn't seem to be working, there's no change to the <a> tag, and also no javascript errors :/ When using $(this), it wouldn't matter that theres loads of other class="save_places" would it?
thebluefox
@thebluefox - I haven't fully woken up yet :) give this a try for your success `success: $.proxy(function(msg){ $(this).replaceWith(msg); }, this)`
Nick Craver
Boom there we go! Absolutely spot on mate, much appreciated. And come on! I've been up 6 hours already!
thebluefox
A: 
$(".save_places").click(function() { 
  var self = this;

  $.ajax({ 
    url: "{/literal}{$sRootPath}{literal}system/ajax/fan_bus.php", 
    type: "POST", 
    data: ({id : this.getAttribute('id')}), 
    dataType: "html", 
    success: function(msg){ 
      $(this).html(msg);
    } 
  }); 
  return false; 
});
jAndy
This would put the content inside the `<a>`, making it a big hyperlink that saves on the next click again.
Nick Craver
durr, right. .replaceWith() is just fine then
jAndy