Karthik
Karthik

Reputation: 377

Assign a dynamic string to the ID of an element

I want to assign a dynamic string to the ID of an element. Here's how my input element is:

<input id = 'ac'+ @item.index />

notice, I am assigning 'ac' + @item.index

@item.index is the dynamic part, it is an integer.

I can assign @item.index and it works, however, when I try to combine the string 'ac' to @item.index, it does not work.

What am I doing wrong?

Thank you.

Upvotes: 1

Views: 481

Answers (4)

vittore
vittore

Reputation: 17579

I would use string.Format here

   <input id='@string.Format("ac{0}", item.index)' />

This way it would be crystal clear what exactly you are doing.

Upvotes: 0

Travis J
Travis J

Reputation: 82267

The issue is that ac is going to be a plain string in html. So is what follows it (the +).

Instead, you should just plug it right in (remember it will literally write right there)

<input id = 'ac@(item.index)' />

Upvotes: 0

Brandon
Brandon

Reputation: 69973

Try using an explicit code nugget. Basically just wrap it in brackets. You'll also want quotes around your attribute value.

Try this

<input id="ac@(item.index)" />

Upvotes: 3

Jordan
Jordan

Reputation: 2039

Try changing the integer to a string then combining them

Edit sorry the first one I posted was was for Java, here's the javscript

'ac' + @item.index.toString();

Upvotes: 1

Related Questions