Scipt for FF & IE

function test(evt)
{
if (document.all)
{
var cell = event.srcElement;
}
else
{
var cell = evt.srcElement;
}

    if (cell.parentElement.grid1=="ch")
    {

}
}

i have teh above function
if i just usse
var cell = event.srcElement; i get errro in FF as event not defined
but if i use the above function with if (document.all) and all

then i get cell is undefined

The standard way that’s commonly used is:


function test(evt) {
    evt = evt || window.event;
    var targ = evt.target || evt.srcElement;
    if (targ.nodeType === 3) { // is text element
        targ = targ.parentNode(); // fix Opera bug
    }
    if (targ.nodeType === 1) { // is node element
        // do stuff here
    }
}

i get error function test(evt)
{
evt = evt || window.event;
var targ = evt.target || evt.srcElement;
if (targ.nodeType === 3)
{ // is text element
targ = targ.parentNode(); // fix Opera bug
}
if (targ.nodeType === 1)

{ // is node element
// do stuff here

    // var cell = targ.srcElement;
    if (targ.parentElement.grid1=="ch")
    {
        document.getElementById('H_text').value=targ.innerText;
        var tempX, tempY;
        if (document.all)
         {
            tempX = targ.clientX;
            tempY = targ.clientY;
         }
         else
         {
            tempX= evt.pageX+"px";
            tempY = evt.pageY+"px";
   	 }

document.getElementById(‘H_X’).value=tempX;

}

}

}

Error: targ.parentElement is undefined
Source

and in IE i get undefined alert instead of getting some cordinates

The parentElement object is not properly supported. Use parentNode instead, which you can see in the example code.

Also, instead of checking for document.all, it’s better to check if the non-IE property exists and to use that. If it doesn’t, you can use the iE property instead?

if (typeof evt.pageX !== 'undefined') {
    ...
} else {
    ...
}

function test(evt)
{
evt = evt || window.event;
var targ = evt.target || evt.srcElement;
if (targ.nodeType === 3)
{ // is text element
targ = targ.parentNode(); // fix Opera bug
}
if (targ.nodeType === 1)

{
// var cell = targ.srcElement;
if (targ.parentNode.grid1==“ch”)
{
document.getElementById(‘H_text’).value=targ.innerText;
var tempX, tempY;
if (typeof evt.pageX !== ‘undefined’)
{
tempX = targ.clientX;
tempY = targ.clientY;
}
else
{
tempX= evt.pageX+“px”;
tempY = evt.pageY+“px”;
}
document.getElementById(‘H_X’).value=tempX;
alert(document.getElementById(‘H_text’).value);
alert (tempX);

}
}

}

IN FF

i dont get any alert & noothing get executed

in IE alert (tempX); i get undefined

That’s because you have the parts of your if/else around the wrong way.

If pageX is not undefined, that’s when you want to use pageX, not clientX as you currently have.

ok
i will check that out now

secondaly i wanted to know how do i get the cordinates
currently i get the cordinates of the screen click ie X & Y
instead of the getting the screen coordinates when clicked in need to get the coordinates /position of the cell which is clicked
i have table with columns & i clcik the table header cell
need cordinates of it

You may want to use info from http://www.quirksmode.org/js/events_properties.html which gives you the mouse coordinates relative to the document.
From there, you can get the element position in realation to the document, and ate the difference to get the mouse position in relation to the element itself.

You may want to use info from http://www.quirksmode.org/js/events_properties.html which gives you the mouse coordinates relative to the document.
From there, you can get the element position in realation to the document, and ate the difference to get the mouse position in relation to the element itself.

function test(evt) {
evt = evt || window.event;
var targ = evt.target || evt.srcElement;
if (targ.nodeType === 3) { // is text element
targ = targ.parentNode(); // fix Opera bug
}
if (targ.nodeType === 1) { // is node element

    // var cell = targ.srcElement;
    if (targ.parentNode.grid1=="ch")
    {
        document.getElementById('H_txt').value=targ.innerText;
        var tempX, tempY;

              if (typeof targ.pageX !== 'undefined')
               {

            tempX= targ.pageX+"px";
            tempY = targ.pageY+"px"; 
            } 
            else 
            {
            
            tempX = targ.clientX;
            tempY = targ.clientY;
            }
            alert (document.getElementById('H_txt').value);      
            alert (tempX);      
          document.getElementById('H_X').value=tempX;
          document.getElementById('btnpb').click(); 
        
    }
}

}
this is teh code

in FF
nothing happens nor do i get error in error console nor do i get any alerts
nor any processing happens

in IE i get teh value of alert (document.getElementById(‘H_txt’).value);
but alert (tempX); i get undefined
& the button click event happens

function doSomething(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information

document.getElementById(‘btnpb’).click();
}

in window.onload function i have
document.getElementById(‘grid’).onmousedown = doSomething

here th eproblem is even if i use scroll back the function gets activated
i just want that when the cell is clciked the function shld get activated
i tried with .onclcik event
but nothing happens

What are your eventual plans with this?

To do some drag and drop type of stuff with the cell, or something else?

no i dont want any drag and drop type
currently lots of things r done but with some issues
hope it gets cleared

  1. FF issue i mean the script that i have doesnot work with FF
    2)i have a grid with few columns & the grid has scroll bar
    the grid does not fit in one screen so need to scroll the browser
    when a grid header column is clicked a div control gets displayed nr that column & works fine with browser scroll
    when the browser is scrolled the div control moves with the column
    but if i use the grid scroll bar the div control doesnot move with the column

hope the query/issues r clear

in short

i found a sample at

my requirement is similar but with a difference that i have only one div

here they have a menu bar in my case its table header cells

which when clicked i get the header cell content by javascript

i have only one div which shlould be displaced on the header cell click

rest all things needs to be done with data from database

i hope i am clear with my query

Yes indeed. It seems that you have no need to know the mouse position at all, and instead are intend on using the header cells to provide sorted information.

if i dont get the mouse position how do i place the control in the column in which column header was cllicked

By using the clicked header as your reference.