This is a migrated thread and some comments may be shown as answers.

Access to jQuery .data() for repeated components

1 Answer 51 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Derek Hunziker
Top achievements
Rank 1
Derek Hunziker asked on 08 Oct 2010, 10:59 PM
Hello,

When I have a component in the view with a basic ID/Name like "MyNumericTextBox" I can access the jQuery object just fine using:

alert($('#MyNumericTextBox').data('tTextBox').value());

However, for repeated controls, my ID structure must match the following:

Item[0].Price
Item[1].Price
Item[2].Price

... but I am unable to access the data object using:

alert($('#Item[0].Price').data('tTextBox').value());
// or
alert($('#' + $('div[name$=Price]').attr('id')).data('tTextBox').value());

Any help would be much appreciated!

Thanks,

-Derek

1 Answer, 1 is accepted

Sort by
0
Derek Hunziker
Top achievements
Rank 1
answered on 10 Oct 2010, 08:45 PM
Turns out jQuery does not like square brackets in the ID attribute and it's not valid HTML markup to boot. The solution is to set the ID via the HtmlAttributes setting to something friendly to jQuery:

.Name(String.Format("Item[{0}].Price", rowNum))
.HtmlAttributes(new { id = String.Format("Item_{0}_Price", rowNum) })
Tags
General Discussions
Asked by
Derek Hunziker
Top achievements
Rank 1
Answers by
Derek Hunziker
Top achievements
Rank 1
Share this question
or