Jump to content

Jquery Function Not Working


sf_guy

Recommended Posts

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();
});

Link to comment
https://forums.phpfreaks.com/topic/270476-jquery-function-not-working/
Share on other sites

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() { .. });

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.