Setting id attribute with knockoutjs including prefix

I use KnockoutJS to iterate over an object, for example:

Now it all works. But the problem is that it sets the id for button only a number. So it looks like this:

 <button id="1">Button 1</button> <button id="3">Button 2</button> <button id="8">Button 3</button> 

So, I tried to set the prefix before the Id property, for example:

 <div data-bind="foreach:Items"> <button data-bind="text: Name, attr: {'id': 'myprefix_' + Id}"></button> </div> 

But this does not seem to work. My id populated with some observable Knockout function when I do it like this ...

So my question is: how can I add a prefix when I specify the id attribute of a field?

+60
Sep 20
source share
4 answers

If Id is observable, you should "expand" it: 'myprefix_' + Id() .

+54
Sep 20 '12 at 8:23
source share

In fact, it was used today - to expand the observables I had to:

 <button data-bind="attr: { id: 'prefix_' + $index() }"> Send </button> 

Hope this helps.

+57
Dec 19 '13 at 1:13
source share

I think it is best to use the index $ index

 <div data-bind="foreach:Items"> <button data-bind="text: Name, attr: {id: 'myprefix_' + $index() }"></button> </div> 
+19
Nov 14 '13 at 19:32
source share

  <img data-bind="event: {click: $root.afficherDetailmembreFamille}" src="ucc/gestion_Famille/images/arbre-fleche-off.png" /> <label data-bind=" text: nom"></label> <label data-bind=" text: prenom, click: $root.afficherDetailmembreFamille"></label> <br> <div data-bind="attr: {'id': 'DivMembreFamille'+id}" style="margin-left: 40px; display: none;"> 
0
Apr 19 '17 at 9:23 on
source share



All Articles