2

I have two hyperlinks on my page: previous and next which navigate to the previous and next pages respectively. I want to disable previous when I am at the first page and disable next when I am on the last page.

To try this out I used the following code (showPage is hardcoded here).

 $("#prevUp").click(function (event)
        {

         event.preventDefault();
        var showPage=0;
         if(showPage<=1)
         {
            $("a").removeAttr('href');
         }

   });

However this is not working. Would you please tell me how I can do this (I have already looked at previous solutions on this problem but could not find one that works for me).

EDIT:

look at http://jsfiddle.net/bhXbh/38/ I tried several solutions here but none are working.

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
Romi
  • 4,833
  • 28
  • 81
  • 113
  • possible duplicate of [jQuery disable a link](http://stackoverflow.com/questions/970388/jquery-disable-a-link) – Pekka Jan 14 '14 at 21:01

8 Answers8

1
jQElement.prop('disabled',true);
David Brossard
  • 13,584
  • 6
  • 55
  • 88
Beri
  • 11,470
  • 4
  • 35
  • 57
1

You can do

$("a").attr('href', 'javascript:');
Alex Ackerman
  • 1,341
  • 12
  • 14
  • Alex Ackerman: its not working. look at jsfiddle.net/bhXbh/37 – Romi Jul 20 '11 at 06:53
  • It performs all the javascript before you set the href attribute to 'javascript:', but it will not go to the link specified in the href, if it's there. Although I would agree that using 'disabled' attribute is better. – Alex Ackerman Jul 20 '11 at 07:39
1

try the below

$("a").attr('disabled', 'disabled');
Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
1

You can also try

$("a").click(function (e) {
    e.preventDefault();
});
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Svetlin Panayotov: check this, and tell me what i am doing wrong http://jsfiddle.net/bhXbh/37/ – Romi Jul 20 '11 at 06:57
1

Normally, by calling

event.preventDefault();

The browser doesn't load the page...

ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
1

Let us suppose u have total 10 pages. Update your pagination like when we click on page 10 , href value for next link would be javascript:void(0) and when we click on first page previous link href value will be javascript:void(0). And when your page loaded first time previoud link value will be same as javascript:void(0). Use simple if else condition to check your page no. You can send your page no in querystring.

key2
  • 428
  • 1
  • 4
  • 11
1

please see below for the code that I think your looking for:

$(document).ready(function(){

    var currentPage = <current page here>
    var pageTotal = <total number of pages>

    if(currentPage<=1)$('#previous').attr('disabled','disabled');
    if(currentPage>=pageTotal)$('#next').attr('disabled','disabled');

});

May I suggest you do this in a server side language if your loading pages you simply do it all in a server side language and do it as the DOM is being sent to the client, as opposed to letting the DOM load client side and doing the necessary actions (seen above) because it will mean the user can, if they wanted to, fiddle with the buttons.

Flux
  • 391
  • 2
  • 14
0

$("a").attr('disabled', true);

Vivek
  • 10,978
  • 14
  • 48
  • 66