Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:43:15+00:00 2026-06-08T22:43:15+00:00

I have several textbox elements being generated in a foreach, like this: @foreach(var thing

  • 0

I have several textbox elements being generated in a foreach, like this:

@foreach(var thing in Model.Things)
{
    <tr>
        <td>@Html.TextBoxFor(m => thing.StartDate, new { id = "thingStartDate", @class = "thingDatePicker" })</td>
    </tr>
}

I’m attaching jQuery datepickers to each textbox, like this:

$(document).ready(function(){
    $(".thingDatePicker").datepicker({ dateFormat: "mm/dd/yy" });
});

The datepickers all fire correctly on each textbox, in the correct positions. But when I select a date from any of them, it only populates the first textbox. How do I make sure each datepicker is firmly associated with its textbox?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:43:18+00:00Added an answer on June 8, 2026 at 10:43 pm

    You seem to be using the same id for all your textboxes (id="thingStartDate") which obviously results in invalid HTML. So either remove the id attribute or make sure you don’t get dupes. I would probably remove it as you are using a class selector and this id serves no purpose at all:

    @foreach(var thing in Model.Things)
    {
        <tr>
            <td>
                @Html.TextBoxFor(
                    m => thing.StartDate, 
                    new { @class = "thingDatePicker" }
                )
            </td>
        </tr>
    }
    

    Also I hope you realize that m => thing.StartDate will generate incorrect names for those textboxes. It will use name="StartDate" instead of name="Things[xxx].StartDate" which might get you into troubles later when you attempt to bind back to your view model. For this purpose I recommend scraping the foreach loops in view in favor of editor/display templates:

    @Html.editorFor(x => x.Things)
    

    and inside the corresponding editor template (~/Views/Shared/EditorTemplates/Thing.cshtml)

    @model Thing
    <tr>
        <td>
            @Html.TextBoxFor(
                x => x.StartDate, 
                new { @class = "thingDatePicker" }
            )
        </td>
    </tr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This other SO question asks about an autocomplete textbox in WPF. Several people have
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I have a form that is generated based on several DataTemplate elements. One of
I have a form that posts several like named elements to an action like
On one of my ASP.NET pages I have several asp:textbox fields. On this page
I have several <asp:TextBox TextMode=MultiLine> elements on a page. On load, I populate them
I have several Textboxes generated via a foreach in the view. To make each
I have several textareas like: <asp:textbox class=input ThisIsRussia ..... /> <asp:textbox class=input ..... />
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>
I have several UITableViews and what I would like to do is to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.