Reputation: 601
I've recently started using Ajax and javascript.
This is my ajax function:
var saveForm = function () {
var form = $(this);
$.ajax({
url: form.attr("action"),
data: form.serialize(),
type: form.attr("method"),
dataType: 'json',
success: function (data) {
if (data.form_is_valid) {
w=window.open();
w.document.write($("#book-table tbody").html(data.html_book_list));
w.print();
w.close();
$("#book-table tbody").html(data.html_book_list);
$("#modal-book").modal("hide");
}
else {
$("#modal-book .modal-content").html(data.html_form);
}
}
});
return false;
};
In the output it only displays [object Object]
and no html is getting passed.
How can I pass correct html to the document.write() and how can I access the "data" to perform modification?
Upvotes: 1
Views: 1539
Reputation: 1515
$('#element').html()
returns a string of the content that exists at the target.
$('#element').html('some html')
returns the HTML object that has the new code added to it.
$('#output').html( $('#test').html() );
// returns a string of the HTML inside
console.log( $('#test3').html() );
// returns the object being targeted
console.log( $('#test2').html('some html') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
<div id="test">Test content 123</div>
<div id="test2">Test Content 456</div>
<div id="test3">Test Content 789</div>
Upvotes: 2