views:

28

answers:

1

Hi, I'm using codeigniter and when selecting a dropdown form onchange works and i get my second field ok(Country->City). When i submit the form with errors, the page reloads with the errors displayed but my onchange stops working. Any ideas for what's going on? Hmm, here's what i'm doing. User get's to homepage and fills the form, the onchange is working. User then submits the form with an error and i check the validation and load a new page(register) with the same onchange function. I tried to change the function name (getLocal) to another name but the result is the same. Tried to use live.("change", getLocal) and the result is the same, no firing on the register page...

 
function getLocal(){
    $("#city").load("home/ajaxlocal", {country: $(this).val()} );
   //alert($(this).val());

    return false;
}

..............

$js = 'id="country"';
    
    $(document).ready(function() {
    $("#country select").bind("change", getLocal);
        });
    
td echo form_label('Country :', $country); 
td id="country">echo form_dropdown('country', $country,$ct,$js);
+1  A: 

IF yo arent reloading the page, but reloading elements with AJAX, the jquery will not bind to those new elements if you specify them in the ready() function.

$(document).ready(function() {
    $("#country select").bind("change", getLocal);
});

This is loading this onload - try using .live()

http://api.jquery.com/live/

This method is a variation on the basic .bind() method for attaching event handlers to elements. When .bind() is called, the elements that the jQuery object refers to get the handler attached; elements that get introduced later do not, so they would require another .bind() call.

Kieran Andrews
This sounds right to me.
musoNic80
Hmm, here's what i'm doing. user get's to homepage and fills the form, the onchange is working. User then submits the form with an error and i check the validation and load a new page(register) with the same onchange function. I tried to change the function name (getLocal) to another name but the result is the same. Tried to use live.("change", getLocal) and the result is the same, no firing on the register page
JEagle
Can you post us your complete code for both the homepage and register page, plus you ci controllers and your jquery script? It's a bit difficult to see exactly what's going on at the moment...
musoNic80
Well, now I feel stupid...Just forgot to add the id="country" to this line on the register page: td id="country">echo form_dropdown('country', $country,$ct,$js);Sorry for waisting your time :(
JEagle
No problem, it helped solve your issue.
Kieran Andrews