Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Set val on a dynamic dropdown

Posted on 2014-12-13
5
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
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

715 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