reza
reza

Reputation: 155

jQuery .filter() dont work

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

Answers (2)

Arun P Johny
Arun P Johny

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

Jon
Jon

Reputation: 437376

Your current code will not work for three reasons:

  1. You are injecting everything in msg into your element as HTML; .filter only runs after this has happened, so it cannot possibly work.
  2. .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.
  3. The matched set in this case does not include any #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

Related Questions