Get HTML content by outerHTML and remove one div

332
December 19, 2016, at 10:26 PM

I am getting content of HTML tag by using outerHTML

var t=$("html")[0].outerHTML;

but I need to remove a specific div from result

<div id="admin_panel">...</div>

here is my jQuery code:

$(function() {
    $('#save').click(function(){
        var t=$("html")[0].outerHTML;
        $.ajax({
            type: "POST",
            url: "save.php",
            data: { code: t },
            cache: false,
            success: function(html){
                alert('Changed saved');
            }
        });
    })
});

How do I remove the div correctly?

Answer 1

Non-destructive:

jQuery: outer html()

var $div = $('<div/>').append($('html').clone()).html();
$div.find("#admin-panel").remove();
var t=$div.html();
Answer 2

Looks like that:

$(function() {
    $('#save').click(function(){
        var t = $("html")[0].outerHTML;
        var $t = $(t);
        $t.find("#admin_panel").remove();
        t = $t[0].outerHTML;
        $.ajax({
            type: "POST",
            url: "save.php",
            data: { code: t },
            cache: false,
            success: function(html){
                alert('Changed saved');
            }
        });
    })
});
READ ALSO
Data binding the function on click with multiple parameters knockout

Data binding the function on click with multiple parameters knockout

I have searched the net and tried different approaches mentioned but the click function sometimes fires and sometimes it doesn't

310
Why there is a NaN?

Why there is a NaN?

Before click:

269
Multiple File Upload jquery ajax with extension

Multiple File Upload jquery ajax with extension

I have problem in uploading multiple file upload using jquery/ajax

377