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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:01:43+00:00 2026-06-11T09:01:43+00:00

i am attempting to do a dynamic select here using rails and jquery. the

  • 0

i am attempting to do a dynamic select here using rails and jquery. the code is as follows

<div class = "line_items">  

<%- calc = Hash[Item.all.map{|p| [p.id, p.quantity]}].to_json %>

<div class = "item"><%= f.collection_select :item_id,Item.all,:id,:title, :prompt => "Select a Item", input_html: {data:{calc: calc} %></div>

   <div class ="quantity"> <%= f.text_field :quantity %></div>
/*rest  of code*/
</div>

javascript for the view is as follows

jQuery(document).ready(function(){
    jQuery('.item').bind('change',function() {
    var selectElement = jQuery(this);
    var  itemSelected = jQuery('.item:selected').val();
    var wrapperDivElement = selectElement.parent(".line_items");
    var quantity= eval(selectElement.data("calc"))[itemSelected];
    jQuery(".quantity", wrapperDivElement).val(quantity);
  });
});

when i change the item i am getting the following error
eval(selectElement.data("calc"))[itemSelected] is undefined in firebug. Can anyone point out where i am going wrong? also any better way to get the quantity. i feel the method i am doing is crude. any guidance would be helpful. Thanks in advance.

  • 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-11T09:01:45+00:00Added an answer on June 11, 2026 at 9:01 am
    jQuery(document).ready(function(){
        jQuery('.item select').bind('change',function() {
        var selectElement = jQuery(this);
        var  itemSelected = selectElement.val();
        var wrapperDivElement = selectElement.parents(".line_items");
        var quantity= eval(selectElement.data("calc"))[itemSelected];
        jQuery(".quantity input", wrapperDivElement).val(quantity);
      });
    });
    

    i guess the value of itemSelected was not getting detected, hence the error. the above redefined code should work. But i strongly urge you not to get data like this. its better to do a json call or ajax call to get the related data from the controller. and use parents instead parent in the code:)

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

Sidebar

Related Questions

I'm new to JQuery + JQuery mobile. I am attempting to create dynamic div
I'm attempting to use JQuery to create a dynamic page header using an image
I'm attempting to get over the hurdle of dynamic form fields in Rails --
I'm building an MVC3 app for my dynamic web class, and while attempting to
I'm attempting to create a dynamic map using the latest Google Map API. Everything
I am attempting to implement a WPF ViewModel using Castle Windsor Dynamic Proxies. The
I am attempting to create a dynamic query using expression trees in LINQ to
Using Ruby 1.9.2 and Rails 3 - When attempting to use <%= error_messages_for 'story'
I'm attempting to build some dynamic menus that are created on the fly by
I'm attempting to perform dynamic sorting of data that I'm putting into grids into

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.