All right folks I have a problem with some jQuery error handling code I have written. The issue is, if I write the jQuery code so that PHP injects it into the page using echo statements the code works just fine. However if I just enter the jQuery code after the end of the php statements and let it be included as if it were just normal html it fails to work. I suspect that there is some sort of issue with the elements not being available in the DOM when the jQuery actually makes it back to the browser but I'm not sure how this could be the case.
As for the code here are the examples.
This is how the code is written if it is included after the ending ?> tag of the php in the include file.
<script>
$("#adminuserlist select[name=userlist]").change(function(){
if($("#adminuserlist select[name=userlist]").val() == \'\'){
$("#noadminuser").css("visibility", "visible");
$("#noadminuser").dialog({
modal:true,
show: {
effect: "fade",
duration: 300
},
hide: {
effect: "fade",
duration: 300
}
});
}
else{
$("#adminuserlist").submit();
}
}
);
</script>
This is how the code looks when I inject it with PHP. It works if I do it this way, but it is, in my mind a bit of an organizational nightmare.
<?php echo '<script>';
echo '$("#adminuserlist select[name=userlist]").change(function(){ if($("#adminuserlist select[name=userlist]").val() == \'\'){ $("#noadminuser").css("visibility", "visible"); $("#noadminuser").dialog({ modal:true, show: { effect: "fade", duration: 300 }, hide: { effect: "fade", duration: 300 } }); } else{ $("#adminuserlist").submit(); }});';
echo '</script>'; ?>
When I examine the result source in the browser from both of these configurations they seem identical beyond the spacing. If this is some sort of issue with the elements not existing in the DOM when the jQuery is loaded I would love to hear a way to fix it. I have tried the delegate binding method using on. I have tried encapsulating the code in a $(document).ready statement, and a number of other methods but it seems to ignore all of my efforts when I do not inject the jQuery code using PHP.
What baffles me the most is I have a similar page that uses a and the .click method instead of a with the .change method that works perfectly when the jQuery code is included as straight HTML after the PHP code. I'm stumped so any input would be great.
Thanks in advance.
ifstatement looks a bit strange, it should beif ($("#adminuserlist select[name=userlist]").val() == '')) { ... }You have some extra backslashes.