Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/AJAX read html inside div tag

Tags:

html

jquery

ajax

I'm getting HTML from a database via AJAX but when I output that HTML into a div, it only shows the actual HTML text, not the pictures and such as a browser sees it. Anyone know how to do this? I'm outputting the HTML in textarea #html and want to see it rendered in div #preview. Here's the code:

$(document).ready(function(){
    $('#creative').change(function() {
        $.ajax({
            url: '/app/components/MailingsReport.cfc',
            //POST method is used
            type: "POST",
            //pass the data 
            data: {
                method: "getCreativeHTML",
                creativeID: $('#creative').val(),
                datasource: "shopping_cart"
                 },
            dataType: "html",
            //contentType: "application/text; charset=utf-8",
            success: function(response){
                var obj = $.trim(response);
                //alert("response");
                if (obj == '"0 records"') {
                    $('#preview').html("No creative found.");
                }
                else { 
                    $('#html').val( obj );
                    $('#preview').html( obj );
                }
            }
        })
    });
});
like image 999
user1431633 Avatar asked Jan 25 '26 19:01

user1431633


1 Answers

I see you answered your own question. You could also consider the jQuery .load() method, which is designed for this sort of thing.

like image 97
Steve Campbell Avatar answered Jan 27 '26 10:01

Steve Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!