Solved

Ajax Return

Posted on 2016-08-16
6
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

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 9

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 58

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 110

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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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 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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

623 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