Solved

Ajax Return

Posted on 2016-08-16
6
58 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 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 52

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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)

912 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

16 Experts available now in Live!

Get 1:1 Help Now