Jump to content

[SOLVED] Strange JS Behavior?


MrFish

Recommended Posts

It looks like the moveTabTo function is the problem. That function sets the HTML content of the container when you click on a tab, and when it does that it replaces whatever elements were there with the new content, which results in the event handler you added to that element removed along with the element. It works to set the tab click event on load because the tabs never go away. It's also why using the attribute works, because the attribute is part of the HTML code. If you want to see that in action, click to the last tab and type something in the To field, then click on any other tab and back. You'll notice the text you typed is gone. I haven't looked at the other tabs to see what other problems that behavior might cause.So, that's the problem. Other than using the attribute like you did, other solutions would be to change how the tabs work so that they show and hide elements instead of deleting and creating, or attach your event handler in the tab change code. Ideally the tab change would be an event that you can put an event handler on in that case, but it's occasionally more efficient to show and hide elements instead of creating them on the fly.

Link to comment
Share on other sites

Ah that makes so much sense! Thank you for your help justsomeguy. Now my faith in cause and effect is restored. I was beginning to think JS had supernatural properties. haha

Link to comment
Share on other sites

The knowledge that computers never do something without a reason helps me debug. When I was looking at that, one of the first steps to find the solution was to realize that setting the event handler succeeds, which I can verify using breakpoints, and also that setting it with the same code through the console works, assuming you're on that tab. So if you know that at the start it was successfully attached, and that it doesn't run, then the answer must be that something is removing it. Everything with computers happens because of a specific reason, something told the computer to do that. The worst programmers I've worked with have assumed that there's not a path to get to the answer. One guy referred to bugs in his code as "anomalies" and basically stopped trying to fix them. Debugging may be the most important skill a programmer can have, right up there with the ability to do research about a problem. You can't fix a problem without knowing what the problem is, and you can't find a solution if you assume that things happen for no reason. Things like this can get tricky with no error messages, but you just need to start listing and verifying the facts you know, and look for causes that fit with those facts. The key word in that sentence, by the way, is "verifying". It's easy to get into a dead end when you start assuming things.

Link to comment
Share on other sites

Ah that makes so much sense! Thank you for your help justsomeguy. Now my faith in cause and effect is restored. I was beginning to think JS had supernatural properties. haha
or even divine properties ! :)
The knowledge that computers never do something without a reason helps me debug. ... ....but you just need to start listing and verifying the facts you know, and look for causes that fit with those facts. The key word in that sentence, by the way, is "verifying". It's easy to get into a dead end when you start assuming things.
it is, after all, computer science. :) is it just me, or does the world of computers rarely get referred to as such, you hear "high-tech", or "computer tech" and because we forget(?) it is a science, we just explain away any failures as "anomalies" as if it were a condition of nature that humans have yet to understand fully.
Link to comment
Share on other sites

Right I was making a joke. I knew there was a reason but I was stumped. I don't really believe in computer anomalies :)Thanks for the debugging advice.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...