Solved

Ajax Return

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

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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 55

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is my wamp get_include_path() wrong? 2 19
PHP error function not working on AWS 10 65
jQuery when a text link is clicked? 16 37
Accessing specific element - DOM PHP HTML 8 34
This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

789 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