Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 3rd, 2008, 08:51 AM
Member
 
Join Date: Oct 2007
Location: israel, Tel Aviv
Posts: 95
Default move div tag with javascript

hello ppl - long time....

i was wondring how i can get the position of a <div> tag,
i want to move it to the left or right on a press of a button

please help
thanks
Reply
  #2  
Old September 3rd, 2008, 08:59 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 599
Default

You can accomplish that by changing the CSS properties (good candidates are: position, left, right, top, bottom, padding and margin). An element's CSS property is accessible via [element].style.[cssProperty].

regards
Reply
  #3  
Old September 10th, 2008, 08:26 AM
Member
 
Join Date: Oct 2007
Location: israel, Tel Aviv
Posts: 95
Default

thanks man for your fast reply and sorry for my late one :(

still thats not working for me
here is what i done:

file.cs:
[CODE=css]div{
background:#999999;
width:200px;
height:100px;
}
div.test{
left:100px;
}[/code]
javascript function:
[CODE=javascript]function Find(x){
//alert(document.getElementById(x).style.position);
pos = document.getElementById(x).style.position;
document.getElementById(x).style.position = pos + 150;
}[/code]thats my main page:
[HTML]<body>
<div id="mydiv" class="test">
text.text.text.text.text.text.text<br />
<a href="#" onClick="Find('mydiv');">move</a></div>
</body>[/HTML]

what am i doing wornge here :\
thanks
Reply
  #4  
Old September 10th, 2008, 09:43 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 599
Default move div tag with javascript

Quote:
Originally Posted by Amzul
[CODE=javascript]function Find(x){
//alert(document.getElementById(x).style.position);
pos = document.getElementById(x).style.position;
document.getElementById(x).style.position = pos + 150;
}[/code]
what am i doing wornge here :\
thanks
I think you mixed position with (say) left. values for element.style.position are: absolute, relative, static, fixed. you probably meant element.style.left. and you forgot the 'px'.

note: Trying myself (FF 3.01/Mac OS 10.4.11) I had no problems setting the element's style this way, but I could get the settings only through:
[CODE=javascript]var elem = document.getElementById("_elem_id_");
var cs = window.getComputedStyle(elem, null);
var val = cs.left; // only works for absolute values (i.e. no em, ex, %)[/code]
dunno how it is with IE
FF/Opera can also use the methods getPropertyValue() / getPropertyCSSValue() (DOM level 2 - style)

regards
__________________
the best way to find a solution—switch off the computer and go home

Last edited by Dormilich; September 10th, 2008 at 09:58 AM. Reason: additional info
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles