tags:

views:

51

answers:

2

Hello,

I am doing an ajax call to a server side function that returns an html response made only of two DIVs, like for example

<div id="divOne">text on divOne</div>
<div id="divTwo">text on divTwo</div>

Can I parse the markup response to extract only the text part included in the DIVs?

+2  A: 

If your success callback, say it's function(data) {}, you can use the returned data as a context to look in, for example:

success: function(data) {
  //if these elements are nested down in a response, in another element
  var combinedText = $("#divOne, #divTwo", data).text();
  //if your response contains *only* these elements
  var combinedText = $(data).text();
  //of again if it contains *only* these elements and you want to filter which
  var combinedText = $(data).filter("#divOne").text();
}

The format is just $("selector", context), whatever your ideal selector is.

Or in the case that your response contains only what's in the question, and they're root level elements, you need to .filter() instead...since $("selector", context) is a $(context).find("selector") internally, it won't find any elements at the root level in the returned data.


Or if you're using .load() specifically, you can use a selector on the end to get that entire element, like this:

$("#something").load("myPage #elementToGet");
Nick Craver
ive never done it like this. learn something new every day
Pavan
@Nick Craver: are you sure that this way it works? In my case inside a success callback if I use your suggestion the "combinedText" variable is empty whereas the data passed contains the markup that I wrote in the question...
Lorenzo
@Lorenzo - Perhaps I misunderstood, if that's *all* it contains just `$(data).text();` works, since they're root level elements...or if there are others, then `$(data).filter("#divOne").text()` for example to just get the first `<div>`.
Nick Craver
@Nick Craver: I used this way: `var title = $("#divOne", response).text(); var message = $("#divTwo", response).text();`.
Lorenzo
@Lorenzo - I updated to make this a bit clearer, if your response contains *only* these elements and they're not inside anything, you'll need `$(response).filter("#divOne").text()` to get the element/text you're after.
Nick Craver
@Nick Craver: Thank you! Also my question was not so clear... :)
Lorenzo
@Lorenzo - Welcome! I would update the question though, saying your response contains only that content...may help the next guy finding this :)
Nick Craver
+1  A: 

You can use this regular expression that will strip out html tags and leave just the data you require:

 String noHTMLString = htmlString.replaceAll("\\<.*?>","");

You will get

inputString.split('\n')  # --> ['Line 1', 'Line 2', 'Line 3']

This is identical to the above, but the string module's functions are deprecated and should be avoided:

import string

string.split(inputString, '\n')  # --> ['Line 1', 'Line 2', 'Line 3']

Alternatively, if you want each line to include the break sequence (CR,LF,CRLF), use the splitlines method with a True argument:

inputString.splitlines(True)  # --> ['Line 1\n', 'Line 2\n', 'Line 3']

Hope this helps if you need any more help do let me know.

PK

Pavan
@Pavan: this would give me only one string whereas I need two....
Lorenzo
nicely spotted. i will alter the code right away
Pavan