Reputation: 155
I have a html page that contain a form with id "frmCmt", i want to just show this form in other document, i using jQuery .filter() but now work and show full page.
my jQuery code:
function showCommentForm(postID){
$.ajax({
type: "GET",
cache: true,
url: "http://mysite.com/post/comment/"+postID,
success:(function(msg){
$("#commentContainer"+postID).html(msg).filter("#frmCmt") ;
return false;
})});
please help.
Upvotes: 1
Views: 293
Reputation: 388316
You may need to use find()
instead of .filter() here, filter will work only if the given element is a top level element in msg
One way to fix it could be
$(msg).find('#frmCmt').appendTo($("#commentContainer"+postID).empty())
Or you can use the .load() function
$("#commentContainer"+postID).load("http://mysite.com/post/comment/"+postID + ' #frmCmt')
Upvotes: 0
Reputation: 437376
Your current code will not work for three reasons:
msg
into your element as HTML; .filter
only runs after this has happened, so it cannot possibly work..filter
filters items from the matched set of a jQuery object, but tou are not using the result of .filter
to do anything so again it would not work.#frmCmt
element so .filter
will evaluate to an "empty" jQuery instance; even if that were somehow used the results would not be what you expected.Change the code to
var html = $(msg).find("#frmCmt").html();
$("#commentContainer"+postID).html(html);
This code creates a jQuery object from the contents of msg
and zeroes in on the required element with .find
before inserting it into the DOM.
Upvotes: 1