ram esh
ram esh

Reputation: 259

Javascript - keep the button disabled on all pages

I have a javascript function as below.

$("#update").click(function (){
             this.disabled = true;
            $("#kwBody > tr").each(function() {
               var $cells = $(this).children("td");

               var found=false,count=0,currentCell;
               for (var i=0;i<masterData.length;i++) {
                 currentCell=$cells.eq(i+1);
                 found = parseInt(currentCell.text(),10) >=masterData[i];
                 currentCell.toggleClass("found",found); //add or remove class to highlight 
                 count+=found;
               }
               window.console && console.log(masterData,count);
               $(this).toggle(count==masterData.length); // show if all cells >

            });
        });
});
$("slider:changed")

Once I click on the button for updating the values, I am trying to disable the button. However, since I am using pagination, if I navigate to the second page, my button re-enables again. Is there any way to keep it disabled across all the pages?

This is the link to my work so far.

Upvotes: 0

Views: 103

Answers (2)

Rob Gibbons
Rob Gibbons

Reputation: 1613

Use localStorage, or a cookie, to store the "true/false" value of the button. Check that value on every page load and assign its value back to the button's disabled property.

In your click handler, after "this.disabled = true", add:

localStorage.setItem("updateDisabled", true);

Then check for the value again on page load:

$(function () {
    var disabled = localStorage.getItem("updateDisabled");
    if (disabled) $('#update').attr('disabled', disabled);
});

Upvotes: 2

elixenide
elixenide

Reputation: 44841

You need to do one of two things: (1) pass the variable back to your server in some way, or (2) pass it through to the next page. You can do (1) with AJAX or a cookie, and you can do (2) with a URL parameter or a cookie. The web is "stateless," meaning (among other things) that each page doesn't know anything about what just happened on another page, unless you pass that information along somehow.

Upvotes: 1

Related Questions