AJAX response:
<div id="div_1">Some text</div>
<div id="div_2">Some text</div>
<div id="div_3">Some text</div>
Now I need to remove some divse, before the result is shown to users. So lets for example remove div_1 and div_3:
var result = $(ajax_response).find('#div_1, #div_3').remove();
Now we can show it to users:
$('#result_div').html(result);
But it doesn't work - nothing will show up. What am I doing wrong?
EDIT: Working solution, but I don't like, that I have to show the result before a change it:
$('#result_div').html(ajax_response).find('#div_1, #div_3').remove();
AJAX response:
<div id="div_1">Some text</div>
<div id="div_2">Some text</div>
<div id="div_3">Some text</div>
Now I need to remove some divse, before the result is shown to users. So lets for example remove div_1 and div_3:
var result = $(ajax_response).find('#div_1, #div_3').remove();
Now we can show it to users:
$('#result_div').html(result);
But it doesn't work - nothing will show up. What am I doing wrong?
EDIT: Working solution, but I don't like, that I have to show the result before a change it:
$('#result_div').html(ajax_response).find('#div_1, #div_3').remove();
Share
Improve this question
edited May 22, 2014 at 16:58
Koralek M.
asked May 22, 2014 at 16:37
Koralek M.Koralek M.
3,3614 gold badges31 silver badges32 bronze badges
3
- So all three DIVs are still showing? – Dean.DePue Commented May 22, 2014 at 16:42
- The ajax response are not jquery elements yet, the result es in a string and you append to your html right? – pecci Commented May 22, 2014 at 16:44
- I solved it by this answer stackoverflow./questions/14546749/… – Aya Mostafa Commented Aug 19, 2019 at 15:23
1 Answer
Reset to default 7find()
only works within the context of an element. The three elements you receive are all siblings, so you can either wrap them in a container on the server side:
<div class="container">
<div id="div_1">Some text</div>
<div id="div_2">Some text</div>
<div id="div_3">Some text</div>
</div>
Or you can do that programmatically in your JS:
var $container = $(ajax_response).wrap('<div />').parent();
$container.find('#div_1, #div_3').remove();
$('#result_div').html($container);
Example fiddle