Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    basic question about function writing

    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:

    Code:
    <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.
    Last edited by thellie; 09-07-2010 at 08:05 AM. Reason: NEW INFO

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    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:

    Code:
    $(function() {
    // do stuff
    });
    is equivalent to

    Code:
    $(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:

    Code:
    $(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?

  • Users who have thanked Spudhead for this post:

    thellie (09-07-2010)

  • #3
    New Coder
    Join Date
    Aug 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •