When I console.log(data)
from my ajax callback, it returns the entire html file which includes scripts, css etc which breaks my page. How can I get only the element block which I need? I only need the data within .container
.
Asked
Active
Viewed 141 times
0
-
4Possible duplicate of [jQuery selector regular expressions](http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions) – afzalex Dec 09 '15 at 04:54
-
1in ajax success try to use function(response){ alert($(response).find('.container').html()); – Mohamed-Yousef Dec 09 '15 at 04:55
-
2Why don't you just return the necessary things from ajax? – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 09 '15 at 04:58
-
@TareqMahmood that is correct. – Parth Trivedi Dec 09 '15 at 05:02
-
@TareqMahmood if I can control the output it's better in json, but it's external api – Jennifer Dec 09 '15 at 06:03
3 Answers
0
I would do this using regular expressions.
For example, if you wanted to get the content of all .container
elements, you could try this regexp:
<[^>]+class="container"[^>]*>([^<]+)<\/[^>]+>
and then collect the captured groups.
See working example on Regex101.com.

Jonathan Lam
- 16,831
- 17
- 68
- 94
0
I think easiest way is to use jquery load function. There is possibility to take only fragment of loaded page.

Brzochod
- 1
- 2
0
Easiest way would be append in hidden tag. Then search for using $(".container") it will return the whole div and you can get html . Here is script code that can help you (use jquery library)
var b= $(".container");
console.log(b[0].outerHTML);
PLEASE CHECK THIS BELOW FIDDLE
http://jsfiddle.net/oyvv9nL0/

Dashang G. Makwana
- 377
- 3
- 10