When the time return from ajax, I should return as json encode, and use jquery.parseJSON and use document.createElement and append the data inside the Element that just created.
or it is better to return as html text?
example,
<div id="contentcontainer"></div>
$.ajax({
type: "POST",
url: "some.php",
data: "name=John",
success: function(msg){
msgObj = jquery.parseJSON(msg);
var div = document.createElement('div');
div.style.color="red";
$(div).append(msgObj.name);
$('#contentcontainer').append(div);
}
});
//some.php
if($_POST['name']){
echo json_encode( array('name'=>$_POST['name']) );
}
OR I should do like this?
<div id="contentcontainer"></div>
$.ajax({
type: "POST",
url: "some.php",
data: "name=John",
success: function(msg){
$('#contentcontainer').append(msg);
}
});
//some.php
if($_POST['name']){
echo '<div style="color:red">'.$_POST['name'].'</div>';
}
Ammended... sorry for my bad grammar
Of course, this is just a example, real case it would have a lot of data, may be in html table format.
Of course, this is just a example, real case it would have a lot of data.
if it has a lot of data, then I need to write a lot of document.createElement(). and it consumes time to write like this document.createElement('table'); document.createElement('tr');
instead of (Return as HTML and just append in the container)
For me I think second format( return HTML ) is easier.
But not sure for the performance wise, which is better?
Please advise.
document.createElement,$('#xx').append(data)your code will be longer and hard to read, compare to simply html markup<table>? Am I getting the point?