Weird issue

T

Tom

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?
 
W

weston

Tom said:
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?

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.
 
T

Tom

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...
 
L

Lee

Tom said:
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?

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

web.dev

Tom said:
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?

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

Tom

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

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top