Link to home
Start Free TrialLog in
Avatar of Berhan Karagoez
Berhan KaragoezFlag for Sweden

asked on

HTML DOM structure gets "messed" up after JS function call?

I have back-end engine creating html, resulting in a webpage with frames. So far everything works fine.

A recent addition (javascript function) that changes the background cell of a table in the same frame (called "output", which is the big part of the screen where everything is sent to), works fine, ie does the job BUT after the function call, any click on links from the 'top' frame opens a new tab, instead of loading the link content to the 'output' frame, as it does normally when the new function is not called.

The function that breaks things is: colorTableStat, called with click on an image, like this:
<img src="/images/colorize.png" alt="colorize table" onclick="colorTableStat(''tab11'');colorTableStat(''tab22'');colorTableStat(''tab33'');flip=!flip;" ALIGN=right width=46 height=26/>'              

tab11, tab22, tab33 are ID's on a TD where the actual table with the rows/data is and is being colored correctly. so the coloring function works fine besides it messes up things after it has been called so the links no longer open in the 'output' frame.

the video upload does not work so here is a link to a video version showing what the problem is:
https://www.dropbox.com/s/1izle622xoz3kuz/lost_focus.mov?dl=0

Any idea what might be wrong?
color.js
color.js.pdf
0---normal.png
1---colorized.png
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

My first guess would be event propagation.  To test the theory, you could adjust your JS function so that it accepts the event, and then do a stopPropagation();

Here is more on the topic: https://davidwalsh.name/javascript-events
Avatar of Berhan Karagoez

ASKER

I dunno, I mean I have a similar function that does the same thing with another set of tables and that one does not break the logic, why should another similar function break it?!

I will have to check and test things, will have to get back on this, but I am not happy to invent stuff to make a "bug" stop being a bug. I don't see the logic of manipulating the dom breaking the links in another frame, or making the whole site "forget" this being the 'output' frame.

Read some on the link provided, not sure how to test that one.
I am sending just the id's of the <TD>'s, those are being set on and off with colors. The code is very basic.

I don't get the things said in the link you provide zephyr_hex.
Again the main 'output' frame works fine before and after the call to the JS function, it is the top frame links that behave differently after calling to the colorTableStat(id) function. Which is very similar to another function colorTable(id), actually a copy of it and adjusted to the columns of this set of tables being manipulated. Those two functions are pretty similar in logic, and I don't understand why one does nothing to the link behaviour (of the top frame) while the other breaks it and forces links now opening to a new tab. it's like the main 'output' frame is somehow forgotten that it exists?!

I don't get that.
ASKER CERTIFIED SOLUTION
Avatar of Berhan Karagoez
Berhan Karagoez
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry but the selection of "assisted solution" and "best solution" does not work, probably buggy javascript of some kind, I can not close this question...
You can flag the question for moderation attention and let them know you can't close it.  Also, since my answer was completely down the wrong path, you should just mark (or tell the Mod to mark) your solution as the answer.
As a side note, I tend to suspect event propagation when odd things happen.  Event propagation can cause iterations of an event to fire, leading to crazy results.  If you're often working in JavaScript, it's important to understand the concept of events "bubbling" up the DOM.  stopPropagation prevents the event from "bubbling" up any further.
Thanks for your time zephyr_hex!!
This fixes the original issue.