JavaScript - Can't Decrement Mydiv.offsettop
I've got a div whose position I want to decrement, but it's not working:
myDiv.offsetTop--; doesn't work. I'm using offsetTop because myDiv.top is undefined, myDiv.style.top is "". myDiv.offsetTop is the only field I've found that has a valid value. But when I tell it myDiv.offsetTop-- the value doesn't change. If I tell it myDiv.offsetTop = myDiv.offsetTop - 1, I get no results either (although if I do an alert on myDiv.offsetTop - 1, it gives me the expected value; why this value can't be assigned back to myDiv.offsetTop is a mystery). Similar Tutorialsi have a problem with this code when working in IE it gives me 0,0 coords when i use iframe tag and -1,-1 when i use div tag but gives correct coords when using firefox Code: unction GetRealOffset(id) { var elem = document.getElementById(id); var leftOffset = elem.offsetLeft; var topOffset = elem.offsetTop; var parent = elem.offsetParent; while(parent != document.body) { leftOffset += parent.offsetLeft; topOffset += parent.offsetTop; parent = parent.offsetParent; } var Offsets = new Object(); Offsets.top = topOffset; Offsets.left = leftOffset; alert(Offsets.top + " " +Offsets.left) return Offsets; } i tried this code as well Code: function findPosX(obj) { var curleft = 0; if (obj.offsetParent) { while (obj.offsetParent) { curleft += obj.offsetLeft obj = obj.offsetParent; } } else if (obj.x) curleft += obj.x; return curleft; } function findPosY(obj) { var curtop = 0; if (obj.offsetParent) { while (obj.offsetParent) { curtop += obj.offsetTop obj = obj.offsetParent; } } else if (obj.y) curtop += obj.y; return curtop; } but both r giving same value in IE can u help me plz thx in advance Edit/Delete Message Hi I need a table like below http://img25.imageshack.us/img25/9568/screenshotkoc.jpg I am new to javascript and have several ideas after going on the w3c website but can't seem to glue the ideas together. Any help appreciated. I have a pool of stats point (say 20) that can be assigned to various attributes which have starting values (say 8) . I want a text box for the starting value and images for plus or minus to decrement or increment the stats whilst modifying the pool of overall stats points. So far my ideas are to use onmouseclick and I have written if $a ($tb ++ && $p -- while $p>0); if $b ($tb -- && $p ++ while $p<20); I have the idea to define the plus image as $a, minus image $b the pool of stats $p and the text box with starting value (which I have to find out how to set) as $tb. I have no clue if this will work or if I've got muddled with php and am struggling to glue it together with the form inputs and onmouseclick. My form is as follows (class="tb" is from the css file and applies style to the textboxes): Code: <div id="stats" > <font color="#6B50C1" face="achafont" size="2"> <table class="statstable" border="0" cellpadding="10"> <tr ><td>Deftness</td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" name="deftness" size="1" class="tb"/><img src="images/plus.jpg" alt="plus" /></td></tr> <tr ><td>Charm</td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" size="1" name="charm" class="tb" /><img src="images/plus.jpg" alt="plus" /></td></tr> <tr ><td>Wit</td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" name="wit" size="1" class="tb" /><img src="images/plus.jpg" alt="plus" /></td></tr> <tr ><td>Magika</td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" size="1" name="magika" class="tb" /><img src="images/plus.jpg" alt="plus" /></td></tr> <tr ><td>Clarity</td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" name="clarity" size="1" class="tb" /><img src="images/plus.jpg" alt="plus" /></td></tr> <tr ><td>Ruthlessness </td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" size="1" name="ruthlessness" class="tb" /><img src="images/plus.jpg" alt="plus" /></td></tr> <tr ><td>Stealthiness </td><td><img src="images/minus.jpg" alt="minus" /><input type="textbox" size="1" name="stealthiness" class="tb" /><img src="images/plus.jpg" alt="plus" /></td></tr> <tr><td colspan="2">Points to spend=xx</td></tr> </table><br /><br /> </font> </div> |