Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:49:14+00:00 2026-06-04T20:49:14+00:00

How can I disable and enable an anchor tag with this custom binding. It

  • 0

How can I disable and enable an anchor tag with this custom binding. It works great with input elements but the anchor tag just changes the CSS, not the disabling.

<a href="link" data-bind="myDisabled: !enabled()"/>

ko.bindingHandlers.myDisabled = {
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        ko.bindingHandlers.css.update(element, function() {return { disabled: value }; });
        ko.bindingHandlers.disable.update(element, valueAccessor);
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:49:16+00:00Added an answer on June 4, 2026 at 8:49 pm

    You need to capture click event in your binding handler.

    HTML:

    <a href="link" data-bind="disableClick: !enabled()">test</a>
    <br/><br/><br/>
    <input type="checkbox" data-bind="checked: enabled"> enabled ​
    

    JavaScript:

    ko.bindingHandlers.disableClick = {
        init: function (element, valueAccessor) {
    
            $(element).click(function(evt) {
                if(valueAccessor())
                    evt.preventDefault();
            });
    
        },
    
        update: function(element, valueAccessor) {        
            var value = ko.utils.unwrapObservable(valueAccessor());
            ko.bindingHandlers.css.update(element, function() {return { disabled_anchor: value }; });
        }
    };
    
    ko.applyBindings({ enabled: ko.observable(false)});
    

    ​

    Here is a working example:

    http://jsfiddle.net/kp74u/54/

    UPDATE 1: If you need to prevent other event handlers bound after knockout binding handler was attached, you need to add stopImmediatePropagation to the event handler along with preventDefault.

    example: http://jsfiddle.net/kp74u/55/

    UPDATE 2: If you want to disable all event handlers (along with click event handlers attached before your binding handler, you need to ‘hack’ the jquery events array). Please note that this may not work other versions of jquery (example uses 1.7):

    ko.bindingHandlers.disableClick = {
        init: function(element, valueAccessor) {
    
            $(element).click(function(evt) {
                alert('test before');
            });
    
            $(element).click(function(evt) {
                if (valueAccessor()) {
                    evt.preventDefault();
                    evt.stopImmediatePropagation();
                }
            });
    
            //begin of 'hack' to move our 'disable' event handler to top of the stack
            var events = $.data(element, "events");
            console.log(events);
            var handlers = events['click'];
    
            if (handlers.length == 1) {
                return;
            }
    
            handlers.splice(0, 0, handlers.pop());
            //end of 'hack' to move our 'disable' event handler to top of the stack
    
    
            $(element).click(function(evt) {
                alert('test after');
            });
        },
    
        update: function(element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            ko.bindingHandlers.css.update(element, function() {
                return {
                    disabled_anchor: value
                };
            });
        }
    };
    

    example: http://jsfiddle.net/nickolsky/kp74u/40/

    UPDATE 3: As was mentioned there (suggested edit by FIR55TORM, sorry can’t approve this completely correct edit because I am too late to review): if you’re using jQuery 1.10.x, you will need to add an underscore to access the ‘data’ object like so:

    var events = $._data(element, "events"); 
    

    Revised fiddle for jQuery 1.10.x: http://jsfiddle.net/nickolsky/kp74u/41/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see that you can enable/disable using the EnableWindow method, but how do I
I can disable one and one by doing this: jQuery('#ListBoxA').attr('disabled','true'); But how can I
I am using the following code to disable the anchor tag.Can anyone tell me
I just want to enable / disable onclick and href on elements (a or
I have a anchor tag which I would like to disable or enable depending
I can get the form id with form_id= $(this).parents('form').attr('id'); I can disable/enable ALL the
I need to enable/disable a row columns of a datagridview this can be easily
I have a screen where you can enable/disable modules for my Android application. For
plz guide me how I can enable or disable asp.net validation controls using jQuery
I have a validator which I can enable or disable using the following javascript:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.