Functions will not execute onclick

Discussion in 'JavaScript' started by ksquared3, Feb 3, 2011.

  1. #1
    Two functions called by a nav link will not execute. They are declared in the <head> area.
    painting.php - main file
    nav_paintings - nav file include

    Thank you v. much for any guidance.

    Here is the code for the functions and variable declarations, declared in the <head>:
    
    <script type="text/javascript">
    var count;
    var imageURL;
    </script>
    <script type="text/javascript">
    function directionBack()
    {
    if (count > 1)
    count = count - 1;
    return count;
    }
    </script>
    <script type="text/javascript">
    function imageToDiv(count)
    {
    imageURL = "<img src=\'images/painting_\' + count + \'.jpg' />";
    document.getElementById("imagediv").innerHTML = imageURL
    return false;
    }
    </script>
    Code (markup):

     

    Attached Files:

    ksquared3, Feb 3, 2011 IP
  2. ksquared3

    ksquared3 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    error message: functions "not defined"
     
    ksquared3, Feb 3, 2011 IP
  3. ksquared3

    ksquared3 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Functions are executing, no errors. I corrected the onBlur code, too. It looks like either count is not being decremented in directionBack() or imageToDiv is not replacing the html. These are the functions as they stand now.
    <script type="text/javascript">
    function directionBack()
    {if (count > 1){
    count = count - 1;
    return count;
    }}
    </script>
    <script type="text/javascript">
    function imageToDiv(count) {
        var imageURL = '<img src="images/painting_' + count + '.jpg" />';
        document.getElementById("imagediv").innerHTML = imageURL;
        return true;
    }
    </script>
    Code (markup):
     
    ksquared3, Feb 3, 2011 IP
  4. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Open your page in Google Chrome browser, then go to menu Tools->Developer Tools. This will open up a console which is useful for debugging javascript stuff. It can tell you if you have a syntax error. You can place a break point in the javascript code so you can step thru the code.
     
    jkl6, Feb 4, 2011 IP
  5. ksquared3

    ksquared3 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey, jkl6, thank you! I've been using Firebug, but it really doesn't handle js debugging all that well.
     
    ksquared3, Feb 4, 2011 IP
  6. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #6
    check the quotes:

    
    <script type="text/javascript">
    var count;
    var imageURL;
    </script>
    <script type="text/javascript">
    function directionBack()
    {
    if (count > 1)
    count = count - 1;
    return count;
    }
    </script>
    <script type="text/javascript">
    function imageToDiv(count)
    {
    imageURL = "<img src=\'images/painting_\'" + count + "\'.jpg' />";
    document.getElementById("imagediv").innerHTML = imageURL;
    return false;
    }
    </script>
    
    Code (markup):
     
    gapz101, Feb 4, 2011 IP