Romi
Romi

Reputation: 4931

How to disable a hyperlink using jquery

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.

Upvotes: 2

Views: 18394

Answers (8)

Beri
Beri

Reputation: 11620

jQElement.prop('disabled',true);

Upvotes: 1

Svetlin Panayotov
Svetlin Panayotov

Reputation: 620

You can also try

$("a").click(function (e) {
    e.preventDefault();
});

Upvotes: 1

Flux
Flux

Reputation: 391

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.

Upvotes: 1

key2
key2

Reputation: 428

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.

Upvotes: 1

ChristopheCVB
ChristopheCVB

Reputation: 7305

Normally, by calling

event.preventDefault();

The browser doesn't load the page...

Upvotes: 1

Vivek
Vivek

Reputation: 11028

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

Upvotes: 0

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

try the below

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

Upvotes: 1

Alex Ackerman
Alex Ackerman

Reputation: 1341

You can do

$("a").attr('href', 'javascript:');

Upvotes: 1

Related Questions