SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Oct 1999
    Location
    France
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question jQuery code only works once - why?

    I am a total jQuery noob, and this is my first serious attempt to do something with it. I have a form with 3 select boxes for day, month, and year. When the day box is changed, I want to show the day of the week in a division. The code I've written works the first time I execute it. But if I change the day a second time, I get an error.

    Here's my document.ready function:
    Code JavaScript:
    $(document).ready(function() {
      // arrival day box is changed
       $('select#a_day').change(function () {
          alert('arrival day was changed to ' + $('.selday').val());
          var theyear = $('select#a_year').val();
          var themonth =  $('select#a_month').val();
          var theday =  $('select#a_day').val();
          alert(theday + '/' + themonth + '/' + theyear);
         dayofweek = weekday(theday,themonth,theyear);
          $('#dowtext').html(dayofweek);
          alert(dayofweek);
       });
    });

    (the alerts are just there for debugging)

    and a couple of helper functions:
    Code JavaScript:
    function calcweekday(adate) {
    var dow = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    aweekday = adate.getDay();
    return dow[aweekday];
    }
     
    // onblur function:
    function weekday(day,month,year)  {
    adate = new Date(year,month-1,day);
    if (adate)  {
    weekday = calcweekday(adate);
    return weekday;
    }
    }
    The error I get is on this line:
    Code JavaScript:
    dayofweek = weekday(theday,themonth,theyear);

    and the error is a type mismatch on the line in red above. So it seems like those three variables aren't valid any more after the first time. Help! Can anyone explain this to me?
    --
    Veronica Yuill
    Archetype IT

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The variables that were declared in $(document).ready() do not exist once the function has finished executing.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Oct 1999
    Location
    France
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh! Um ... OK. So instead of using the variables I should just directly pass the values of the select boxes to my function? I did that in an earlier incarnation, but for some reason it didn't work, while stuffing them in vars first did. But that could have been something else I was doing wrong. I told you I didn't know what I was doing

    What would be a better way of doing it?
    --
    Veronica Yuill
    Archetype IT

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Do you have a test page, or can you provide enough html so that we can put together a local simulation of what you're trying to achieve?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Oct 1999
    Location
    France
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, here you go:

    http://www.archetype-it.com/testjq.shtml

    There are actually 2 sets of dropdowns, but for the moment I've only attached the behaviour to the "From" day, i.e. it only fires when you change the first day box. Ultimately it should work for both sets of boxes.

    Thanks!
    --
    Veronica Yuill
    Archetype IT

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Thank you.

    The trouble lies in the following function:

    Code javascript:
    function weekday(day,month,year)  {
        adate = new Date(year,month-1,day);
        if (adate)  {
            weekday = calcweekday(adate); // the culprit!
            return weekday;
        }
    }

    The line marked "the culprit!" is reassigning the weekday variable that was a function, to be a value instead.

    The next time that weekday is called as a function, the script won't be able to because weekday is not a value instead.

    The solution is one of two different approaches.

    Solution 1 is where you can do without the weekday variable completely and just return the result of the function

    Code javascript:
    function weekday(day,month,year)  {
    	adate = new Date(year,month-1,day);
    	if (adate)  {
    		return calcweekday(adate);
    	}
    }

    Solution 2 is where you declare the weekday variable, so that its scope is limited to only within that function itself.

    Code javascript:
    function weekday(day,month,year)  {
    	adate = new Date(year,month-1,day);
    	if (adate)  {
    		var weekday = calcweekday(adate);
    		return weekday;
    	}
    }

    How that trouble can be avoided is to declare all variables from the top of the function.

    Code javascript:
    function weekday(day,month,year)  {
    	var weekday = '';
    	var adate = new Date(year,month-1,day);
    	if (adate)  {
    		weekday = calcweekday(adate);
    		return weekday;
    	}
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Zealot
    Join Date
    Oct 1999
    Location
    France
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gah! (slaps forehead) Thanks for pointing that out I'll have another play with it today.
    --
    Veronica Yuill
    Archetype IT

  8. #8
    SitePoint Zealot
    Join Date
    Oct 1999
    Location
    France
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, that fixed it! Thanks -- onwards and upwards :-)
    --
    Veronica Yuill
    Archetype IT


Bookmarks

Posting Permissions

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