Click here to Skip to main content
15,881,967 members
Articles / Productivity Apps and Services / Sharepoint

Loading or Refreshing a div Content using JQuery

Rate me:
Please Sign up or sign in to vote.
4.17/5 (11 votes)
5 Aug 2015CPOL 21.5K   5   2
In this post, we will discuss how we can load or refresh some contents to our container div in a particular interval of time.

In this post, we will discuss how we can load or refresh some contents to our container div in a particular interval of time. To do this, we will be using normal setInterval in JQuery. We will create an element which is to be considered as our container div. And by using setInterval, we will refresh the content which we create dynamically to our container div. I hope you will like this.

Using the Code

To work with, load your jQuery reference first. I am using the following version of JQuery.

<script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>

Create a container div

XML
<div id='container'></div>

So now we can do our script part.

Implementation

Please find the below scripts:

JavaScript
$(document).ready(
function() {
setInterval(function() {
var randomnumber = Math.floor(Math.random() * 20);
    var html='';
    for(i=0;i<randomnumber;i++)
    {
        html+='<p>'+ randomnumber+ '</p>';
    }
    $('#container').html(html);
}, 1000);
});

As you can see, we are creating some random numbers which are less than 20.

JavaScript
var randomnumber = Math.floor(Math.random() * 20);

And assigning the random value to the dynamically created p tag.

XML
html+='<p>'+ randomnumber+ '</p>'

Now, we need to style our p tag as follows:

CSS
p {
  border: 1px solid #ccc;
  border-radius: 5px;
  height: 18px;
  width: 18px;
  padding: 5px;
  text-align: center;
  background-color: #999;
  color: #fff;
  float: left;
}

That is all, it is time to run our code.

jsfiddle link: Loading or refreshing a div content jsfiddle

Output

Image 1

Conclusion

I hope you liked this post. Please share your valuable feedback. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I am Sibeesh Venu, an engineer by profession and writer by passion. I’m neither an expert nor a guru. I have been awarded Microsoft MVP 3 times, C# Corner MVP 5 times, DZone MVB. I always love to learn new technologies, and I strongly believe that the one who stops learning is old.

My Blog: Sibeesh Passion
My Website: Sibeesh Venu

Comments and Discussions

 
QuestionjQuery Plugin Pin
Rechousa18-Aug-15 14:48
Rechousa18-Aug-15 14:48 
AnswerRe: jQuery Plugin Pin
Sibeesh Passion18-Aug-15 19:20
professionalSibeesh Passion18-Aug-15 19:20 
That's really great. Thank you so much Smile | :)
==================!!!====================!!!========================
So much complexity in software comes from trying to make one thing do two things.
Kindest Regards
Sibeesh
http://sibeeshpassion.com/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.