Nisman
Nisman

Reputation: 1309

How to capture the value of an element which is being returned by a data-bind attribute?

I am using ajax call and knockout data binding to get the values of an object from backend and showing in the UI. an element like this:

<input id="bill" data-bind="value: billDate">

returns the date for a bill. In the UI I want to format the date so I have a piece of JS code like this:

function formatDate(){
    var billDate = $("#bill").val();
    // the rest of the code
    }

the problem is the JS code cannot capture the value of #bill element after the page is loaded to format it, however the element value already shows up in the UI. What should I do to solve this kind of problem? I tried document.ready function but it did not work for this issue.

Upvotes: 0

Views: 55

Answers (1)

Amadan
Amadan

Reputation: 198304

You can do two approaches:

  • make a computed value, billDateString, which formats billDate(). Use data-bind="value: billDateString".

  • make a custom binding with an update that converts the valueAccessor() into the string and sets it into the element.value, so you can do data-bind="valueWithDate: billDate".

Upvotes: 2

Related Questions