473,499 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems in offsetLeft.

65 New Member
Hi all,

Im trying to do the following:

Have 3 divs which are placed side by side. each has a two links 'left' and 'right'. Say on click of the 'left' hyperlink of the 2nd div(middle), the 1st div should take the place of the middle div, while the middle one should take the place of the 1st place.

I'm using the prop offsetLeft, however have run into some issues. My code is shown below:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Document Title</TITLE>
  5. </HEAD>
  6. <BODY>
  7. <SCRIPT language="JavaScript">
  8.  
  9. function moveDivs(){
  10.  
  11. var movDiv_1 = document.getElementById('ContentMain_1');
  12. var movDiv_2 = document.getElementById('ContentMain');
  13.  
  14. movDiv_1.style.position = 'relative';
  15. movDiv_1.style.left = parseInt(movDiv_2.offsetLeft) + 'px';
  16.  
  17. movDiv_2.style.position = 'relative';
  18. movDiv_2.style.left = parseInt(movDiv_1.offsetLeft) + 'px';
  19.  
  20. //alert("ContentMain_1 top position" + movDiv_1.offsetTop);
  21. //alert("ContentMain_1 left position" + movDiv_1.offsetLeft);
  22.  
  23. //alert("ContentMain top position" + movDiv_2.offsetTop);
  24. //alert("ContentMain left position" +  movDiv_2.offsetLeft);
  25. }
  26.  
  27. </SCRIPT>
  28.  
  29. <Body>
  30.  
  31. <div id="ContentMain" style="float:left;width: 200px; height: 24px; overflow: visible;background-color:#cecece">
  32.     <div style="float:left">Content 1</div>
  33.     <div><a href="#" style="float:right;text-decoration:none">Right</a><a href="#" style="float:right;padding-right:3px;text-decoration:none">Left</a></div>
  34. </div>
  35.  
  36. <div id="ContentMain_1" style="float:left;padding-left:20px;">
  37. <div  style=" width: 200px; height: 24px; overflow: visible;background-color:#cecece">
  38.     <div style="float:left">Content 2</div>
  39.     <div><a href="#" style="float:right;text-decoration:none">Right</a><a href="#" onclick="moveDivs();" style="float:right;padding-right:3px;text-decoration:none">Left</a></div>
  40. </div>
  41. </div>
  42.  
  43. <div id="ContentMain_2" style="float:left;padding-left:20px;">
  44. <div style="width: 200px; height: 24px; overflow: visible;background-color:#cecece">
  45.     <div style="float:left">Content 3</div>
  46.     <div><a href="#" style="float:right;text-decoration:none">Right</a><a href="#" style="float:right;padding-right:3px;text-decoration:none">Left</a></div>
  47. </div>
  48. </div>
  49.  
  50. </BODY>
  51. </HTML>
  52.  
Could anyone please help me out?

Thanks in advance.
Oct 25 '08 #1
3 4899
zaphod42
55 New Member
well...... first of all, you set the elements position to 'relative'...the idea of relative is that it positions it relative to it's siblings....if you could move it, it would no longer be relative, it would have to be 'absolute'.
Expand|Select|Wrap|Line Numbers
  1. movDiv_1.style.position = 'relative';
And since offsetLeft ALWAYS returns a number, (not to mention that when you add 'px' you change it to a string anyway) , the parseInt is not really necessary.

Expand|Select|Wrap|Line Numbers
  1. movDiv_1.style.left = parseInt(movDiv_2.offsetLeft) + 'px';
but if you want the divs to use the relative position to "snap" themselves into place, you might want to consider spans....divs are block elements (they align vertically by default) and spans are inline (horizontal align). You should also look into using the node structure....

the 'relative' position of an element is, as I said before, determined by it's iblings....specifically the ones BEFORE it. look up "insertBefore", that may be what you really need...
Oct 25 '08 #2
Nitinkcv
65 New Member
Thanks a lot. insertBefore was exactly the solution i was looking for. Short and precise.
Oct 25 '08 #3
Nitinkcv
65 New Member
Hmmm.. seems a strange problem with insertBefore in firefox. It rearranges the div only after clicking the link twice.

Found the prob
here

Seems like firefox takes up whitepaces as siblings.
Oct 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2369
by: Pieter Van Waeyenberge | last post by:
Hello In the code below, theres a DIV, a TABLE and another DIV , each with an image in it. When i read out the offsetLeft/Top from imgB in the table , i get offset values relative to the...
11
1995
by: Saqib Ali | last post by:
Please excuse me, this is a fairly involved question that will likely require you to save the file below to a file and open it in a browser. I use Mozilla 1.5, so the problem I describe below...
4
6784
by: Keith Thornhill | last post by:
I'm trying to calculate the top and left distance from the side of the browser of an object (either absolutely or relatively positioned) and i'm running into a weird problem as i'm trying to make...
3
7602
by: ara.pehlivanian | last post by:
I've got a script that determines the true position of an element in the page by cycling up the .offsetParent elements all the way up to the <body> element and tallying up the .offsetLeft values...
10
7706
by: Matt Kruse | last post by:
See: http://www.mattkruse.com/temp/offsetleft.html It appears that the offsetLeft value in IE6 (other versions not tested) incorrectly ignores the border width on a DIV when there is a width:...
2
2096
by: Gary Coutts | last post by:
Hi, I am developing a website using Visual Studio .Net 2003. When reading offsetLeft, in a javascript function, the result is fine when run under I.E. but gives wrong results when run under...
5
5455
by: montybytes | last post by:
Hi there, Although, I have already placed this question in the HTML/CSS section, perhaps it might be worthwhile asking the question here as well. I have a JavaScript function which retrieves...
1
2004
by: ehud37new | last post by:
this script work fine in IE but not in FireFox where is the problem? here is the script /*------------------------------------------------------------------ File: menu.js ...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.