Solved

Ajax Return

Posted on 2016-08-16
6
55 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 7

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 6

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 108

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 51

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 108

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now