Hi fellow coders!
Im new to this forum and fairly new to JavaScript programming. I recently ran into this problem with setting onMouseDown with JS. Please be indulgent with my JS terminology as JS is not my native language.
Thing is I have this intricate loop generating div's from ajax output, for each of these div, I want to assign an onMouseDown function. The JS code I have written looks like this:
8<------------------------------------------------------
...
var e=document.createElement('tbody');
while(i2<i3)
{
var f=document.createElement('tr');
var g=document.createElement('td');
g.setAttribute("id","N"+i2);
g.className="tiny-text";
g.style.cursor="pointer";
g.innerHTML="Number "+i2;
g.onMouseDown=displayDivBox();
f.appendChild(g);
e.appendChild(f);
i2++;
}
...
function displayDivBox(event)
{
var el;
if (!event) event = window.event;
if (event.target) el = event.target;
else if (event.srcElement) el = event.srcElement;
...
}
8<------------------------------------------------------
Now the basic idea would be to generate these div's so that when they are clicked, displayDivBox is called. What actually happens here is that displayDivBox is called when <g> is generated, i.e. event is null or undefined. Moreover, setting onMouseDown this way does not seem to stick to <g> because nothing happens when the div's are clicked.
So, can this be done in some way or did I get something completely wrong?
Im using IE 7.0.5730.11 to run this code.
Any help or hints are greatly appreciated!
Thanks
Im new to this forum and fairly new to JavaScript programming. I recently ran into this problem with setting onMouseDown with JS. Please be indulgent with my JS terminology as JS is not my native language.
Thing is I have this intricate loop generating div's from ajax output, for each of these div, I want to assign an onMouseDown function. The JS code I have written looks like this:
8<------------------------------------------------------
...
var e=document.createElement('tbody');
while(i2<i3)
{
var f=document.createElement('tr');
var g=document.createElement('td');
g.setAttribute("id","N"+i2);
g.className="tiny-text";
g.style.cursor="pointer";
g.innerHTML="Number "+i2;
g.onMouseDown=displayDivBox();
f.appendChild(g);
e.appendChild(f);
i2++;
}
...
function displayDivBox(event)
{
var el;
if (!event) event = window.event;
if (event.target) el = event.target;
else if (event.srcElement) el = event.srcElement;
...
}
8<------------------------------------------------------
Now the basic idea would be to generate these div's so that when they are clicked, displayDivBox is called. What actually happens here is that displayDivBox is called when <g> is generated, i.e. event is null or undefined. Moreover, setting onMouseDown this way does not seem to stick to <g> because nothing happens when the div's are clicked.
So, can this be done in some way or did I get something completely wrong?
Im using IE 7.0.5730.11 to run this code.
Any help or hints are greatly appreciated!
Thanks
Comment