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 7185855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:30:26+00:00 2026-05-28T18:30:26+00:00

I am having trouble selecting a hidden form field value that is a part

  • 0

I am having trouble selecting a hidden form field value that is a part of an array of hidden form field values having the same name. Here’s what one element of the array looks like:

<input data-val="true" data-val-number="The field ProductId must be a number." data-val-required="The ProductId field is required." id="OrderItemViewModels_2__ProductId" name="OrderItemViewModels[2].ProductId" type="hidden" value="3" />

I have tried selecting it several different ways in JQuery, such as:

$('#OrderItemViewModels[2].ProductId').val());

and..

$('#OrderItemViewModels\\[2\\].ProductId').val());

Neither of which worked. I’ve also tried a few other ways as well. So, I tried some of the suggestions from below, hoping to just print the values in an alert. I tried these:

alert('valueDirect2=' + $('#OrderItemViewModels_2__ProductId')).val();
alert('valueDirect1=' + $('input[name="OrderItemViewModels[2].ProductId"]')).val();

This results in printing valueDirect2=[Object object] in both cases. Why? What is wrong here?

I’m at a loss as to how to get these values. Any help?

  • 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-05-28T18:30:28+00:00Added an answer on May 28, 2026 at 6:30 pm

    Do you need this to be dynamic, i.e. change on the fly which input field is selected? If so, you’ll need to add a variable into the middle of your selector.

    If you modify your existing code like this, it’ll work:

    $('#OrderItemViewModels_2__ProductId').val();
    

    Your two samples didn’t work because you’re formatting your selector string similar to how the name attribute, but the # prefix tells jQuery to look for an id attribute.

    It is possible to select an element by name as well, which would look like this:

    $('input[name="OrderItemViewModels[2].ProductId"]').val();
    

    In your alert examples, you’re seeing [Object object] because the $ syntax in jQuery is a shortcut for calling the jQuery function. What you’re really doing is this:

    jQuery('input[name="OrderItemViewModels[2].ProductId"]').val();
    

    It’s just a plain old function call. The S is a convenient shorthand. You’re calling a function and passing in a string, which is the selector that jQuery will evaluate.

    Your selector wasn’t returning anything because it was formatted incorrectly, so you were ending up printing the .val(), or value, of the jQuery function itself. It’s a little confusing until you have a good conceptual grasp of how jQuery works.

    To make your selector dynamic, just splice strings together around the variable that contains the number of the input you want to select:

    $('input[name="OrderItemViewModels[' + myVariable + '].ProductId"]').val();
    

    Here’s a jsFiddle that illustrates both options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I'm having trouble selecting a field only if another fields value is not
I'm having trouble selecting the input value from this html: <li class=t-item> <div class=t-mid>
I am having trouble understanding how to initialize a custom form field in a
I'm having trouble selecting links in my html. Here's the html I have: <div
I'm having trouble selecting the text inside a span element that resides in another
I'm having trouble selecting all contents of the div. Here's a demo: http://jsfiddle.net/KcX6A/304/ It's
I'm having bit of a trouble selecting correct values from my mysql sql server.
I am having trouble efficiently selecting the information I need to display. Hopefully someone
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I'm having trouble selecting a subset of data with doctrine. I have 3 tables

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.