SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Stupid question

  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Winnipeg
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stupid question

    Hello! I have never programmed JS before, so I'm sure my problem is a syntax thing. Or something else stupid and noobish. In any case, please help me.

    OK, what I'm trying to accomplish is a graphic on a web page that turns into a different graphic when clicked, and then another graphic when clicked again... etc. For a total of four different graphics, including the original.

    I thought I could track how many times it was clicked with a variable which would increase every time the image has been clicked, thusly:


    Code:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    var tab1state=1
    var tab1name = "graphic_1.gif"
    
    
    function MM_swapImage() { //v3.0
      var tab1state=tab1state+1
      var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
      if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2]}
      if (tab1state=2){tab1name="graphic_2.gif"}
      if (tab1state=3){tab1name="graphic_3.gif"}
    }
    So the first time it's clicked, it swaps the image with "graphic_1.gif", the next time "graphic_2.gif", and the third time "graphic_3.gif"

    What's happening is the first click changes the graphic to graphic_1.gif properly; the next click goes straight to "graphic_3.gif". It seems to set the tab1name variable to the last one in the function, regardless of the if statement. My problem is syntax, right?

    Can one of you learned masters help me out? Thanks!
    Last edited by Cleeve; Jul 22, 2004 at 13:53.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    assignment != comparison

    Code:
    if (tab1state=2){tab1name="graphic_2.gif"}
    if (tab1state=3){tab1name="graphic_3.gif"}
    http://www.informit.com/content/imag...728958_04.html
    ::: certified wild guess :::

  3. #3
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Winnipeg
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sir, thank you! That is very valuable information.

    I have changed the code to:

    Code:
      if (tab1state == 2){tab1name="a1_scratch.gif"}
      if (tab1state == 3){tab1name="a1_final.gif"}
    Unfortunately, now the graphic does not change at all. Any ideas as to why it wouldn't read the "tab1state" variable as changing to a higher number every time the function is called?

    Or what other follies I may be engaging in?

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you're declaring tab1state - using the var keyword - inside the function. This creates an entirely separate variable, local to the function, which - because of the way variables are looked up ("resolved") - will "mask" the global with the same name. Declare globals once, outside any function, then just refer to them by name.
    ::: certified wild guess :::

  5. #5
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Winnipeg
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Success!

    You are a gentleman and a scholar of the highest magnitude, sir! Your advice has led me away from the path of failure.

    Good day to you! And again, my many thanks!

    - Cleeve


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
  •