Here I am using jQuery ajax:
$.ajax({
type: "GET",
url: URL,
data: { Mode: "POB1"},
success: function (data) {
var Html = $.trim($(data).find("#divpob").html());
if (Html) {
$(Html).find(".lblpob").text("UserName" + Username);
$(".DivRprt").html(Html);
}
}
});
here value of lblpob didn’t get change, but if i use .clone() like this
if (Html) {
var Html2 = $(Html).clone(true);
$(Html2).find(".lblpob").text("UserName" + Username);
Html = Html2;
$(".DivRprt").html(Html);
}
lblpob gets changed.
What difference .clone() is making here ?
There’s an issue with temporary DOM objects and html as string. I’ll break it down line by line:
What your first code does:
Both the call to
.html()and to$.trim()makes sure yourHtmlvar holds a string, not a live DOM object.This turns the
Htmlstring into a new DOM object (that you don’t assign into a var),and change this unnamed DOM object. Not your
Htmlstring.But here you use the original
Htmlstring to change the html ofDivRprt.What your other code does:
After creating a new DOM object and cloning it, you assign it into
Html2,and here you manipulate it.
So here you insert the manipulated DOM object into
.DivRprtMy option:
You don’t need the
.clone(), just keep track of the first DOM object:Or:
Just don’t convert the trimmed data back to string