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 to the CF scene
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation newbee code help please

    I am working with coldfusion and javascript. Am generating a dynamic table with class of each cell being "hdnvc". The cell names are serially 1,2,3 ... etc. When the user clicks on the cell, the javascript function calchg(sel) changes the class for that cell to "hdnvch". So far everything works perfect.

    I have made another function (wcal()) which identifies which cell the user has clicked on by checking the class should be "hdnvch" and accordingly uses that cell name as a parameter for where the link should go. If the user has not selected any cell, which means all cells have the class "hdnvc", I have tried to put in a clause (else if ) that if the loop reaches the last cell without encountering any cell with class "hdnvch", then go to a different link. The moment I add this clause, the function starts ignoring the first clause (if) and always goes to the link in the second clause (else if). If I take off the clause (else if), the link goes to the parameter of the selected cell, but then if no cell is selected, the link doesn't work.

    I am sure I am making a very obvious error in coding. I have no experience in javascript and I made these functions reading through this forum.

    I will be very grateful for any advise, pointers, suggestions, guidance. The two functions are listed below. Thx in advance. Please ignore the coldfusion parameters enclosed within # #. They work alright.
    <cfoutput>
    <script type="text/javascript">

    function calchg(sel) {
    for (var i=1; i<32; i++) {
    var theElement = document.getElementById(i);

    if (i == sel) {
    theElement.className = 'hdnvch';
    }
    else {
    theElement.className = 'hdnvc';
    }
    }
    }
    </script>
    <script type="text/javascript">
    var thismonth;
    thismonth = #DaysInMonth(CreateDate(url.ye,url.mo,1))#;
    function wcal() {
    for (var i=1; i<=thismonth; i++) {
    var theElement = document.getElementById(i);
    if (theElement.className == 'hdnvch') {
    location.href = 'weeklycal.cfm?mo=#url.mo#&ye=#url.ye#&da=' +i;
    }
    else if (i == thismonth)
    {
    location.href = 'weeklycal.cfm?mo=#url.mo#&ye=#url.ye#&da=1';
    }
    }
    }
    </script>
    </cfoutput>

  • #2
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macintosh found solution, don't understand the logic

    Resolved the issue with hit&trial.
    In the function below if I replace
    (i == thismonth) with (i == 1), everything works alright. I haven't understood the logic. If someone can shed some light on this, I'll be grateful !!!

    <cfoutput>
    <script type="text/javascript">
    var thismonth;
    thismonth = #DaysInMonth(CreateDate(url.ye,url.mo,1))#;
    function wcal() {
    for (var i=1; i<=thismonth; i++) {
    var theElement = document.getElementById(i);
    if (theElement.className == 'hdnvch') {
    location.href = 'weeklycal.cfm?mo=#url.mo#&ye=#url.ye#&da=' +i;
    }
    else if (i == thismonth)
    {
    location.href = 'weeklycal.cfm?mo=#url.mo#&ye=#url.ye#&da=1';
    }
    }
    }
    </script>
    </cfoutput>

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Found the correct solution through another forum. I am posting it here incase someone else has a similar problem, this might help.

    Need to put "return true" at the end of the "if" clause and then the "else if (i == thismonth) clause remains the same.

    Otherwise the function iterates through the loops but returns only the condition at the end of the loop, which is that the "if" clause is false and the "else if" clause is true. So the correct syntax would be

    <cfoutput>
    <script type="text/javascript">
    var thismonth;
    thismonth = #DaysInMonth(CreateDate(url.ye,url.mo,1))#;
    function wcal() {
    for (var i=1; i<=thismonth; i++) {
    var theElement = document.getElementById(i);
    if (theElement.className == 'hdnvch') {
    location.href = 'weeklycal.cfm?mo=#url.mo#&ye=#url.ye#&da=' +i; return true;
    }
    else if (i == thismonth)
    {
    location.href = 'weeklycal.cfm?mo=#url.mo#&ye=#url.ye#&da=1';
    }
    }
    }
    </script>
    </cfoutput>


  •  

    Posting Permissions

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