Last active
June 18, 2018 10:58
-
-
Save bojanbjelic/4556776 to your computer and use it in GitHub Desktop.
Selecting Elements Returned From Jquery Ajax Response Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function () { | |
| $.get('contentUrl', function (response) { | |
| var source = $('<div>' + response + '</div>'); | |
| $('#target').empty().append( | |
| $('<div/>') | |
| .append(response) | |
| .find('#selectorInResult'); | |
| ); | |
| }); | |
| }); |
Author
Good stuff - thanks for the improvements!
Hi, selecting elements returned from Vanilla JS Ajax response strings ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This avoids parsing HTML twice (once via
$(result)and then when inserting it back via.html()):Also, if you receive bad HTML and try to wrap it in
divby concatenation you could end up with fragment with more than one element at the top level. Your find will then miss all topmost siblings. Previous approach ensures the single rootdivto search from.If you are sure your match is at top level this will suffice (no need for wrapping the
divaround the result):$(response).filter('<selector>')