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

  • Home
  • SEARCH
  • 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 8505035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:12:20+00:00 2026-06-11T02:12:20+00:00

I am having trouble getting this to work: http://jsfiddle.net/2jg2F/1/ I basically want to make

  • 0

I am having trouble getting this to work: http://jsfiddle.net/2jg2F/1/

I basically want to make a link that changes to an input box when someone clicks on the text. I have tried knockout 2.0 and 2.1.

HTML

<div data-bind="clickToEdit : message"> </div>

Javascript

ko.bindingHandlers.clickToEdit = {
    init: function(element, valueAccessor) {
        var observable = valueAccessor(),
            link = document.createElement("a"),
            input = document.createElement("input");

        element.appendChild(link);
        element.appendChild(input);

        observable.editing = ko.observable(false);
        ko.applyBindingsToNode(link, {
            text: observable,
            visible: !observable.editing,
            click: function() {
                observable.editing(true);
            }
        });

        ko.applyBindingsToNode(input, {
            value: observable,
            visible: observable.editing,
            hasfocus: observable.editing
        });
    }
};

function ViewModel() {
    this.message = ko.observable("Click on me to edit");
}
ko.applyBindings(new ViewModel());
  • 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-11T02:12:21+00:00Added an answer on June 11, 2026 at 2:12 am

    Your problem is on this line:

    visible: !observable.editing
    

    This will evaluate to false and will stay that way. Even if you do !observable.editing(), then it will be a static true value.

    So, there are a couple of ways to handle it:

    1 – a good choice is to create a quick hidden binding handler that just passes the opposite value to the visible binding. There are a few ways to write it, but here is a simple way:

    ko.bindingHandlers.hidden = {
        update: function(element, valueAccessor) {
            var isVisible = !ko.utils.unwrapObservable(valueAccessor());
            ko.bindingHandlers.visible.update(element, function() { return isVisible; });
        }        
    };
    

    Now, you can use:

    hidden: observable.editing
    

    Here is a sample: http://jsfiddle.net/rniemeyer/2jg2F/2/

    2 – another choice is to add a computed observable that returns the opposite. Something like:

        observable.editing = ko.observable(false);
        observable.notEditing = ko.computed(function() {
           return !observable.editing(); 
        });
    

    Then, binding against notEditing like:

    http://jsfiddle.net/rniemeyer/2jg2F/3/

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

Sidebar

Related Questions

Having some trouble getting this to work... I basically want the report to look
Having trouble getting this to work. What's strange is that I have 10 bookmarks
Ok this seems so silly but I'm having some trouble getting this to work.
I am having trouble getting this invisibilityOfElementLocated() method to work. Here is the code
I'm having trouble getting a sample program to link correctly (in this case against
I am having trouble getting this to work so any help would be appreciated!
I am having trouble getting web services to work in ColdFusion. This works just
I am having trouble getting this to work correctly (obviously) - I am ALMOST
I'm having some trouble getting this to work in a WPF app I'm working
ok so im having trouble getting this to work. nothing is happening. view the

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.