0

Is it possible to retrieve some (in this case) text with an ajax request and modify it before showing it in e.g. a div?

I've got the following code

$.ajax({url: "Files/" + par + ".php",
success: function(result){
    $("#box").html(result);
}

Where par is a parameter (for example foo). This would retrieve the file foo.php and place the obtained file contents in a div called box.

Now I wonder if you could do the following:
Suppose the content of foo.php was as followed:

Some title

some text here...

A sub title

more text....


Is it possible to place some code or a sentence between the title and the text? So the result would be like this:

Some title

This sentence was added
some text here...

A sub title

more text...
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Gooey
  • 4,740
  • 10
  • 42
  • 76

1 Answers1

1

Yes, absolutely.

success: function(result){
    result = result.replace("some text here...", 
        "<i>This sentence was added</i><br />some text here...");
    $("#box").html(result);
}
harriyott
  • 10,505
  • 10
  • 64
  • 103
  • Wow that looks interesting, but what if the text is dynamic (more than one file could be retrieved and this is kind of hard coded)? Could you for example say after the

    tag insert "some text"?

    – Gooey Oct 19 '12 at 15:52
  • Indeed. You can do whatever you like with result. Regular expressions might make for better matching. You could write another function or class to take care of the replacements. Fill your boots! – harriyott Oct 19 '12 at 15:56