Solved

Ajax Return

Posted on 2016-08-16
6
61 Views
Last Modified: 2016-08-16
If I have an AJAX call that return info to different divs, how do I clear those Divs the next time the call is run and there is no ifo to place in the div?  Meaning each time the AJAX runs I want to reset the divs to empty.
0
Comment
Question by:rgranlund
6 Comments
 
LVL 8

Expert Comment

by:James Bilous
ID: 41758161
In the same call in which you do the ajax call, do:

$("#divId").empty();

Open in new window

0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 41758176
Just put the following line of code before your ajax call:
document.getElementById('id_of_your_div').innerHTML="";

Open in new window

or if you're using jQuery:
$('#id_of_your_div').html('');

Open in new window

0
 
LVL 7

Accepted Solution

by:
Mukesh Yadav earned 250 total points
ID: 41758181
As you said you have multiple div then you can assign same class to all of them and before or after AJAX call(as per your logic) you can empty them have a look on the following code

<div class="emptyIt">div 1 content</div>
<div class="emptyIt">div 2 content</div>
<div class="emptyIt">div 3 content</div>
$('.emptyIt').empty();

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41758204
This is not a PHP question; it's a JavaScript question that can be answered with jQuery.

jQuery Documentation Link for empty() method:
https://api.jquery.com/empty/

You might also consider detach() method:
https://api.jquery.com/detach/

Or just set the contents to null with something like this:
$("#dynamic").val("");
0
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41758216
$("#dynamic").val("");
Div's don't have a value - the above line will not work.

Correct answer was the first - empty()

Sample
HTML
  <div class="row">
    <div class="col-lg-6">
      <div id="one">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas facilisis turpis vitae odio mattis, at bibendum purus blandit. Pellentesque metus nisl, rutrum a lectus sed, suscipit fermentum tellus. Quisque vitae massa gravida, rutrum diam in, lacinia neque. Nunc mauris dui, efficitur eget magna in, egestas dictum tortor. Aenean finibus magna nulla, et gravida justo rhoncus sed. In porttitor lorem tortor, vitae dapibus ipsum faucibus ac. Maecenas sit amet orci dui. </div>
      <a href="#one" class="clear-empty">Clear with empty</a>
    </div>
    <div class="col-lg-6">
      <div id="two">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas facilisis turpis vitae odio mattis, at bibendum purus blandit. Pellentesque metus nisl, rutrum a lectus sed, suscipit fermentum tellus. Quisque vitae massa gravida, rutrum diam in, lacinia neque. Nunc mauris dui, efficitur eget magna in, egestas dictum tortor. Aenean finibus magna nulla, et gravida justo rhoncus sed. In porttitor lorem tortor, vitae dapibus ipsum faucibus ac. Maecenas sit amet orci dui. </div>
      <a href="#two" class="clear-html">Clear with html</a>
    </div>
  </div>

Open in new window

JQuery
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
  $('.clear-empty').click(function(e) {
    e.preventDefault();
    $($(this).attr('href')).empty();
  });
  $('.clear-html').click(function(e) {
    e.preventDefault();
    $($(this).attr('href')).html('');
  });
});
</script>

Open in new window

Working sample here
1
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41758221
@Julian: You're right, my mistake.  Divs probably have a .html() method, whereas input controls have a .val() method.

http://api.jquery.com/val/
http://api.jquery.com/html/
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question