4

I have an array of objects in session. This has been populated in select list. based on the selected item from the list, I would have to pre-populate the form with the attributes of the object selected.

Please help.

  • Aanu
2
  • Please provide what server technology you are using; clarify what you mean by "session" (Traditional PHP $_SESSION, etc); Finally, post what you have tried so far. Commented Jan 5, 2010 at 17:44
  • its a java session. I just populated the select as under. #foreach( $emp in $allEmp) <option value= #end on select of one employee I would have to populate the values related to that employee on the form (such as sex, dob, city etc.,). I am trying to do this using jquery. Any help would be great. Commented Jan 5, 2010 at 17:48

1 Answer 1

16

You could store employee information into a javascript object:

var employees = [
    { id: '1', sex: 'm', city: 'Paris' }, 
    { id: '2', sex: 'f', city: 'London' },
    ... etc fill this in a foreach loop
];

Next you bind to the change event of the select box:

$(function()
{
    $('select#id_of_the_employees_select').change(function(e) {
        // Get the current selected employee id
        var selectedEmployeeId = $(this).val();
        // find the corresponding employee in the list
        var emps = $.grep(employees, function(n, i) {
            return n.id == selectedEmployeeId;
        });
        if (emps.length > 0) {
            var employee = emps[0];
            // fill the form elements with employee data:
            $('#employee_sex').val(employee.sex);
            $('#employee_city').val(employee.city);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.