Weird issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    Weird issue

    I've got a weird error that I just cannot figure out and hope that
    somebody has an idea of what to try next. I've got a global variable
    that I am assiging a new value to within a function which is tied to
    the onclick event of a html list. No javascript errors, etc. as a
    matter of fact I can "alert" the value of the global variable at the
    end of the function and sure enough it's contains the new value, and
    everything works like it should. Turn off the alert and the assignment
    doesn't happen?

    Just to add confusion, the global variable will actually contain the
    value of the previously clicked item in the list - if you click on the
    list a 2nd time on the same value, the global variable will "pick up"
    the correct value?


    Driving me nuts, any ideas?

  • weston

    #2
    Re: Weird issue

    Tom wrote:[color=blue]
    > No javascript errors, etc. as a matter of fact I can "alert" the
    > value of the global variable at the end of the function and sure
    > enough it's contains the new value, and everything works like it
    > should. Turn off the alert and the assignment doesn't happen?[/color]

    Are you saying that the alert gives you the correct value, or that
    the entire script works as expected?

    I'm still trying to get the hang of javascript scoping, but know it
    can be tricky and might be able to help if you put up an example.

    In the meanwhile, you could try putting the alert outside the scope
    of the function to make sure that the new assigned value is surviving
    outside of it, or see how it otherwise effects things.

    Comment

    • Tom

      #3
      Re: Weird issue

      If I alert the new value at the end of the function the new assignment
      takes effect globally, if I don't alert the value it doesn't get
      assigned globally until the 2nd time you click on the item in the
      list...

      Comment

      • web.dev

        #4
        Re: Weird issue


        Tom wrote:[color=blue]
        > I've got a weird error that I just cannot figure out and hope that
        > somebody has an idea of what to try next. I've got a global variable
        > that I am assiging a new value to within a function which is tied to
        > the onclick event of a html list. No javascript errors, etc. as a
        > matter of fact I can "alert" the value of the global variable at the
        > end of the function and sure enough it's contains the new value, and
        > everything works like it should. Turn off the alert and the assignment
        > doesn't happen?
        >
        > Just to add confusion, the global variable will actually contain the
        > value of the previously clicked item in the list - if you click on the
        > list a 2nd time on the same value, the global variable will "pick up"
        > the correct value?
        >
        >
        > Driving me nuts, any ideas?[/color]

        Could you post the script (if it's small) or a url so we can actually
        see what's going on?

        Comment

        • Lee

          #5
          Re: Weird issue

          Tom said:[color=blue]
          >
          >I've got a weird error that I just cannot figure out and hope that
          >somebody has an idea of what to try next. I've got a global variable
          >that I am assiging a new value to within a function which is tied to
          >the onclick event of a html list. No javascript errors, etc. as a
          >matter of fact I can "alert" the value of the global variable at the
          >end of the function and sure enough it's contains the new value, and
          >everything works like it should. Turn off the alert and the assignment
          >doesn't happen?
          >
          >Just to add confusion, the global variable will actually contain the
          >value of the previously clicked item in the list - if you click on the
          >list a 2nd time on the same value, the global variable will "pick up"
          >the correct value?[/color]

          The value isn't updated until after the onclick handler fires.
          Try using the onchange event, instead.

          Comment

          • Tom

            #6
            Re: Weird issue

            That did the trick - thank you so much, been banging my head on this
            one for a while.

            Thanks,

            Tom

            Comment

            Working...