0

I want to access the element from a HTML string using JQuery.

I have a following AJAX function which returns HTML text on success. I wish to extract only the contents of the tag from that HTML text using Jquery.

suppose i have a variable resp containing HTML content. I want to access the contents of the tag inside this HTML content as below..

var resp = "<html><head></head><body><form action="changePassword" name="fmChangePassword"> .... form elements .... </form></body> </html>"
alert(resp); //this alerts the full HTML content properly                 
alert($(resp).find("form").html()); //trial 1 -- returns NULL
var test = $(resp).find("#changePassword"); //#changePassword is the ID of the form
//alert(test.html());
displayChangePwdWindow(test);   //need to pass the form content here        

I tried using .find() but no success.

Can anyone help me how to do this?

6
  • what exactly do you want to do?? will give the full code in jsfiddle ? Commented Nov 7, 2012 at 12:13
  • try to alert resp, are you getting the html in that? Commented Nov 7, 2012 at 12:14
  • Can you show your resp content? Maybe something is wrong with it. Because that should work fine: jsfiddle.net/cEBsF Commented Nov 7, 2012 at 12:17
  • Edited the question to make it clearer. Yes, the HTML content is getting alerted properly. Commented Nov 7, 2012 at 12:18
  • I suspect some weird characters (encoding) in your resp that dont allow jquery to parse it as html Commented Nov 7, 2012 at 12:19

3 Answers 3

1

Maybe you need to use .filter() instead of .find(). you can check the Demo: first demo

or maybe if you still want to use .find() you could place the HTML into a wrapper to search from. you can check the Second Demo: second demo

Sign up to request clarification or add additional context in comments.

Comments

0

may be this can help you

$.ajax({
   type: "POST",
   url: url,
   error: function(jqXHR, textStatus, errorThrown) {
      alert(errorThrown);
   },
   success: function(resp){
      var test = jQuery("<div/>").append(resp).find("#changePassword");
      alert(test.html());
      displayChangePwdWindow(test);  //need to pass the form content here    
   }
});

Comments

0

HTML
Create a hidden div in your body....

<div id="DIVID" style="display:none"></div>

AJAX

$.ajax({
        type: "POST",
        url: url,
        error: function(jqXHR, textStatus, errorThrown)
        {alert(errorThrown);},
        success: function(resp){

         $('#DIVID').html(resp);
         var test = $("#changePassword").html();

         displayChangePwdWindow(test);  //need to pass the form content here    
        }
}); 

OR.. you just append it to a body first.. no need to create a hidden div

$.ajax({
        type: "POST",
        url: url,
        error: function(jqXHR, textStatus, errorThrown)
        {alert(errorThrown);},
        success: function(resp){

         $(document.body).append(resp);
         var test = $("#changePassword").html();

         displayChangePwdWindow(test);  //need to pass the form content here    
        }
});

2 Comments

looking at the your it looks like you are adding the complete resp content (i.e. the full HTML content) into the DIV. I just want the <form> content of resp
i am adding the full html content to the div... and using the selector to select the id... $("#changePassword"). which now, will be available in the body... so this should work.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.