Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Set val on a dynamic dropdown

Posted on 2014-12-13
5
Medium Priority
?
168 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 83

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 83

Accepted Solution

by:
leakim971 earned 2000 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

580 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