Solved

Set val on a dynamic dropdown

Posted on 2014-12-13
5
106 Views
Last Modified: 2014-12-14
Experts,

When my user clicks a tablerow, I need to populate a form with the associated data.  

The code below doesn't function in this case b/c the dropdown #product_id is created dynamically from ajax.  

function pop_material(data) {
$.each(data, function(i, items) {
$('#material_id').val(items.materialPK);
$('#product_type').val(items.product_typeFK);
$('#product_id').val(items.productFK); ****// dynamic dropdown (can't set value) ... ****
$('#material_description').val(items.description);
$('#material_quantity').val(items.quantity);
$('#units_in_stock').val(items.units_in_stock);
$('#material_unit_id').val(items.unitFK);
$('#material_price_type').val(items.price_typeFK);
});
}

The html exists as it should, but is invisible to jQuery.  Normally it would link to a JQuery ".on" event.

But here I just need to populate the dropdown with the product_id already stored in the table row!   The user should not have to click.

Help!
0
Comment
Question by:Glen Gibb
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40498773
The code below doesn't function in this case b/c the dropdown #product_id is created dynamically from ajax.  

No, it's not the reason.
ID attribute must be unique

in a loop how do you differentiate the first and the second item if you're using the same reference for them ?
check the case too,

try this :
$('[id=product_id]') instead $('#product_id')
0
 

Author Comment

by:Glen Gibb
ID: 40499173
Tnx for the reply, leakim.

The id IS unique, however.  And all the dropdowns (product_type, material_unit_id, material_price_type) populate as expected.  They are pre-defined in the html.

However, the product dropdown is built after the type is changed.  The code works fine when the user adds a new material item to the job and selects a product type.  The product_id dropdown is populated and the user can complete the remaining fields and submit.

However, here I'm trying to populate the form from a row click event so that the item can be edited (say to change the quantity or to delete the item entirely.)

Here I can't set the value of the dropdown.  All the others populate.  The only thing that separates them is that this one is dynamic.

Maybe I need to pre-build this in php?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40499349
if the dropdown ID is :
- the right one
- unique
- present in the page when the code run

it should work.
could you provide the url of your website ?
0
 

Author Comment

by:Glen Gibb
ID: 40499499
Thanks for the analysis.  I needed that.

All conditions exist, sort of.  The id is correct and unique but not exactly present when the code executes.  I can examine the html in Firebug, but at the code breakpoint, the $('#product_id') ... is undefined.  (This is the problem, no?)

You're welcome to access my website:  Smooth Building Solutions

Login w/ username: glen and pwd: smooth.  Choose Orders > Work Orders.  I have dummy data for client "Galambos, Chuck."  Click the table row and open the "Materials" tab.  Clicking an item from the materials table populates the form.
0
 

Author Closing Comment

by:Glen Gibb
ID: 40499681
Thanks, leakim.

The solution was to build the dropdown earlier - it was not truly "present in the page when the code was run."  I chose to do this in php as part of the original form.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now