Working with knockoutjs attr to write data* attributes
23,246
Solution 1
You just need to put double quotes around it:
<input data-bind='text: Title, attr: {"data-id": "Events[" + viewModel.events.indexOf($data) + "].Title"}'/>
Solution 2
You don't even need to put either double or single quotes around attr name, just go with simply data-id
<input data-bind='text: Title, attr: {data-id: "Events[" + viewModel.events.indexOf($data) + "].Title"}'/>
Related videos on Youtube
Author by
MikeW
Updated on July 09, 2022Comments
-
MikeW almost 2 years
I'm running into a problem with data* attributes in knockout.js ie. writing them out with
attr
.I can do this without a problem:
<input data-bind='text: Title, attr: {name: "Events[" + viewModel.events.indexOf($data) + "].Title"}'/>
but if I want to use
data-id
, the regular way doesn't work so I put a single quote around the attribute:<input data-bind='text: Title, attr: {'data-id': "Events[" + viewModel.events.indexOf($data) + "].Title"}'/>
which gives me
Error: Unable to parse bindings. Message: SyntaxError: missing } in compound statement; Bindings value: attr: { http://127.0.0.1:21254/Scripts/knockout/knockout-2.2.0.js
can someone see what went wrong here?
Cheers!
-
Phil almost 11 years@Michael Best: Thanks, you helped me solve a related problem.