Jquery - get html return text

This is probably simple, but I can not find the answer ...

I have an ajax call that returns html

 dataType: "html"

and he will return. msg looks like this:

<div id="x"><p>Hello</p></div>

and gets the dom add to the top of the parent div:

ajax call...
request.done(function(msg) {
   $('#parent').prepend(msg);
}

The problem is that I SHOULD NOT do this dom manipulation if the text has not changed (since there are other effects that occur when I am truncated. (This is a notification that checks for updates in the interval)

so I want to find "Hello" in msg and compare it with the current xp div, which may already be on the page. This second part is easy to handle, but in the first part I need to do something like msg.text () to just get "Hello", but several options that I tried failed.

pointers?

+5
5

filter(), .

$(msg).filter('div#x p').text(); //this  will give you hello

find()

 $(msg).find('div#x p').text();
+2

$('#parent').prepend($(msg).text());
0

, :

previus

var previusData;

, , :

previusData = $("#x p").html();

DOM , varible

request.done(function(msg) {
   var currData = $("#x p").html();
   if(currData != previusData){
     $('#parent').prepend(msg);
   }
}
0

, "" , , ( ) , , ; , ; ,

var cached_response = '';
ajax call...
request.done(function(msg) {
   if(msg != cached_response){
      $('#parent').prepend(msg);
      cached_response = msg;
   }
}
0

: , html :

$(document).ready(function() {
    var text = '<div id="x"><p>Hello</p></div>';
    console.log(text.replace(/(<([^>]+)>)/ig,""));
});

script .

Here is a working example: http://jsfiddle.net/cGnG5/1/

And another way is using . filter () as said in another answer.

0
source

All Articles