Search code examples
jqueryajaxfilecontentresult

Modify result from ajax request


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...


Solution

  • Yes, absolutely.

    success: function(result){
        result = result.replace("some text here...", 
            "<i>This sentence was added</i><br />some text here...");
        $("#box").html(result);
    }