sf_guy Posted November 8, 2012 Share Posted November 8, 2012 (edited) I'm trying to use the jquery-ui datepicker function in my PHP database script. Here's the definition: <script type="text/javascript"> $('.dateclass').each(function(){ $(this).datepicker(); }); </script> And here is where I try to use it: while($db->nextRecord()){ // walk through all returned rows and create the links // Color code every other line $counter++; if ($counter % 2) { printf("<tr>"); } else { printf("<tr bgcolor='e0e0e0'>"); } // Create editable form printf("<td><input class = 'dateclass' type='text' size='10' maxwidth='10' id='startdatepicker%s' name='startdatepicker%s' value='%s' /></td> <td><input class = 'dateclass' type='text' size='10' maxwidth='10' id='enddatepicker%s' name='enddatepicker%s' value='%s' /></td></tr>", $counter,$counter,$db->Record['start_date'], $counter,$counter,$db->Record['end_date']); } The date fields never drop down. Any ideas what I'm doing wrong. The following sample I found online works just fine:, and the data is filling into the fields correctly from the database <input type="text" class="datepick" id="date_1" /> <input type="text" class="datepick" id="date_2" /> <input type="text" class="datepick" id="date_3" /> script: $('.datepick').each(function(){ $(this).datepicker(); }); Edited November 8, 2012 by sf_guy Quote Link to comment https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/ Share on other sites More sharing options...
gristoi Posted November 9, 2012 Share Posted November 9, 2012 (edited) <script type="text/javascript"> $(document).ready(function(){ $('.dateclass').each(function(index, value){ $(this).datepicker(); }); }); </script> try wrapping it in the document.ready Edited November 9, 2012 by gristoi Quote Link to comment https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/#findComment-1391262 Share on other sites More sharing options...
sf_guy Posted November 9, 2012 Author Share Posted November 9, 2012 Thank you, it's working perfectly now! Quote Link to comment https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/#findComment-1391305 Share on other sites More sharing options...
Zoombat Posted November 10, 2012 Share Posted November 10, 2012 Or a SIAF (Self-invoking-anonymous-function) <script type="text/javascript"> (function() { $('.dateclass').each(function(index, value){ $(this).datepicker(); }); })(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/#findComment-1391570 Share on other sites More sharing options...
Adam Posted November 12, 2012 Share Posted November 12, 2012 Or a SIAF (Self-invoking-anonymous-function) <script type="text/javascript"> (function() { $('.dateclass').each(function(index, value){ $(this).datepicker(); }); })(); </script> Unless that was moved to the end of the <body> tag (or at least to a point after the elements are defined in the HTML) this wouldn't change anything. Did you mean pass an anonymous function to jQuery..? $(function() { .. }); Quote Link to comment https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/#findComment-1391882 Share on other sites More sharing options...
Zoombat Posted November 13, 2012 Share Posted November 13, 2012 (edited) Exactly, that's what I ment, actually it was just a comment on common practice. When you actually use multiple javascripts you don't want global variables, so it's prohibited to enclose your script inside an anonymous function. Let's say there's a global variable inside an external .js file and inside the DOM you redefine this.. what will happen is that you broke the script. So when you can, wrap it inside a anonymous function. Edited November 13, 2012 by Zoombat Quote Link to comment https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/#findComment-1392035 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.