tags:

views:

327

answers:

3

My below code works fine and is used to populate a <select> item with dynamic <options> The problem is that say I end up with 3 <options> say: One / Two / Three and then I select the first <option> so that I can call jquery code to populate the next menu. One is already the <selected> item in the menu so it is not recognized as a onChange event.

So I would need to select Two then One to call the onChange for One.

Is there something I should use instead of onChange? Or should I put in a blank <option> item as the default value? So there will always be a change?

//if the job menu is changed
$('#job').change (function () 
{

 $.get('ajax/employee_menu.php', { job: $('#job').val() },      
    function(data) 
 {
    //load the <options> into the element
  $("#employee").html( data );
   // show the menu once loaded
  $("#employee").css({ "display" : "inline" });
  });
});
+1  A: 

I in fact faced the same issue on a page I was working on last week. I separated the onChange function and called in on document.ready so that it does get invoked immediately with the First selected value.

$(function()
{
    ChangeFunction();
});

$('#job').change(ChangeFunction);

function ChangeFunction()
{
    $.get('ajax/employee_menu.php', { job: $('#job').val() },      
    function(data) 
    {
        //load the <options> into the element
        $("#employee").html( data );
        // show the menu once loaded
        $("#employee").css({ "display" : "inline" });
    });
}
Secret Agent Man
what is that first function my onReady function?
ian
Yeah, the line '$(function()' is shorthand for '$(document).ready(function()'.
Secret Agent Man
Ok I basically see what you did but not good enough with JavaScript and Jquery to change it. Can you alter my code?
ian
Ok i see but that just makes the menu show up on the page load based on the menu its loading from. And also doesn't work if I then change the menu item... When the page loads it loads the #employee menu based on the value of the #job menu. But then if I click the #job menu to change it again the function doesn't call and the #employee menu does not reload.
ian
A: 

Different browsers offer different support for <select /> element events. You can use the onclick event, however it is only supported fully in Firefox. Other browsers won't support it.

You're better off using the onchange event and providing a default option. Alternatively, you could use jQuery to change the <select /> tag to a purely HTML-based combo box, like in this jquery.combobox plugin. That may work better for your purposes.

Drop-downs don't have anything close to consistent support for standard events at the current time (and haven't for the past few years, unfortunately).

Dan Herbert
A: 

You can pre-populate the second select with PHP, so the page renders with OPTION elements for the second select too. That's what I'd do anyway.

Ionuț G. Stan
But each of my menus is based of the previous.. Job -> Shows Employees who can do that job ->> Shows shifts those employees are set as available to work...
ian
@ian, I understand that. But, the first select has its first option selected, so you already know what the second select should contain when rendering the page server-side with PHP. Then when the user selects another option for the first select, the JavaScript will change the contents of the second select.
Ionuț G. Stan
Thats... what it does? The problem is if the menu rendered has the items. Cook Baker Bartender then you have to choose baker. then back to cook if you want to choose cook because the menu will already be the first item rendered. Cook... and thus not a onChange. Or am I missing what your saying completely...
ian
What I'm saying is this. You render with PHP the list cook, baker, bartender, and you already know that cook is the first item in list, so you decide to also render the list associated with cook in the second select. Then, in the browser, the user doesn't have to trigger an onchange event for the cook, because the second element already has the list associated with cook.
Ionuț G. Stan