...

View Full Version : basic question about function writing



thellie
09-07-2010, 01:32 AM
hi,
i'm a complete novice with javascript, but have started incorporating/experimenting with jquery.

as a part of this, i am putting into my page the ui datepicker, which is working fine.

the issue is that i want a number of them on the same page, using different options.

they are already connected correctly to the various fields, but the main ...min.js file is overriding the options i want for each one.

below is the code i have, which i got so far, but the individual options aren't working:


<script type="text/javascript">
$(function() {
$("#dob").datepicker({
yearRange:"c-80Y:c+0Y",
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});
});
$(function() {
$("#passportexpiry").datepicker({
yearRange:"c-0:c+20",
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});
});
$(function() {
$("#M_issue_date").datepicker({
dateFormat: 'yy-mm-dd'
});
});
$(function() {
$("#M_visaexpiry").datepicker({
dateFormat: 'yy-mm-dd'
});
});
$(function() {
$("#M_insurance_expirydate").datepicker({
dateFormat: 'yy-mm-dd'
});
});
</script>

thanks for any help coming my way :)

EDIT: i've downloaded a new pack, with further widgets and a different theme. some of the datepickers now work, without me touching the code - but two of the middle ones (M_issue_date, and, M_visaexpiry) are still not pulling up a calendar (without or without a date inside the dbase). nb. i have the latest jquery version installed.

what i forgot to mention earlier, but i don't think should affect it because it's not going serverside (is it?), is that the datepicker code sits inside a php echo. the scripts above sit below the php code at the bottom of the page.
so i think i can say that the code above works ok, apart from these glitches.

Spudhead
09-07-2010, 10:26 AM
The code you posted looks as though it should work ok. I think the only thing is that you don't need to wrap every datepicker call in a $(function) of its own. Apologies if you already knew this, but:


$(function() {
// do stuff
});

is equivalent to


$(document).ready(function(){
// do stuff
});

and you don't need one for each of the things you want to happen on document.ready - you can put them all into one, like:


$(function() {

$("#dob").datepicker({
yearRange:"c-80Y:c+0Y",
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});

$("#passportexpiry").datepicker({
yearRange:"c-0:c+20",
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});

// etc, etc
});


Regarding what's stopping stuff from working, then... well, it's something else on your page. The fact that it's outputted by PHP shouldn't have any effect, although it's wise to check the generated HTML to make sure a syntax error isn't creeping in. Can you post the whole page outputted HTML?

thellie
09-07-2010, 02:53 PM
thanks for the help - no, i didn't know already... i am an absolute novice to this javascript mullarkey.

anyway. i changed the code into a single function as per your example. no change.

then i stripped out the php code, manually checked and ran through an html validator - nothing wrong. no change.

then i noticed that some of the javascript was single quotes and some were doubles... changed them all to doubles. no change.

then i realised the two not working didn't have id="" inside the input fields. added these in. no change.

then i copied the cell block containing one of the non-working input fields, and pasted it next to another working one. change.

the one i copy/pasted still didn't work, but the original and the other non-working one started working. weird!

so, i've cut the copy/pasted cell block, and everything is fine. for now at least... :confused:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum