Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with knockoutjs attr to write data* attributes

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!

like image 241
MikeW Avatar asked Jan 23 '13 01:01

MikeW


2 Answers

You just need to put double quotes around it:

<input data-bind='text: Title, attr: {"data-id": "Events[" + viewModel.events.indexOf($data) + "].Title"}'/> 
like image 115
Michael Best Avatar answered Oct 20 '22 12:10

Michael Best


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"}'/> 
like image 25
Prasanth K C Avatar answered Oct 20 '22 11:10

Prasanth K C