Set val on a dynamic dropdown

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!
Glen GibbOwnerAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
leakim971PluritechnicianCommented:
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
 
Glen GibbOwnerAuthor Commented:
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
 
Glen GibbOwnerAuthor Commented:
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
 
Glen GibbOwnerAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.