Grow your CSS skills. Land your dream job.

Last updated on:

Partial Page Refresh

Refresh certain elements of a page using jQuery after a set amount of time, can be used with any element with an ID. I amended the example given with the URL to only refresh once and not intermittently. Works in all browsers.

$('#button1').click(function() {

   var url = "http:www.your-url.com?ID=" + Math.random(); //create random number

   setTimeout(function() {
    $("#elementName").load(url+" #elementName>*","");
   }, 1000); //wait one second to run function

});

Reference URL

Comments

  1. Permalink to comment#

    nice stuff,

    Thanks guys.

  2. I have a quick question about this one, since after you do this the DOM treats the refreshed elements as new elements. Since my JS is in separate files how would I reselect the elements in my other script?

    As a basic example lets say I have a lightbox script for the image links within the refreshed element. That script won’t fire after I refresh them. This is because it doesn’t have the ‘new’ elements selected.

    Is there a good way to reselect these for that lightbox script? Would restating the lightbox call within this function do the trick?

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".