473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Please show me how to set this style tag with javascript

Hi, can someone please show me how to set the following conditions for
this DIV tag?

<div style="position:absolute;left:0px; top:0px;" id="changeme">HELLO
WORLD</div>

I want to be able to change the position to 'relative' and the top
position to negative '-100px' with javascript

Thank you! :))

Dec 11 '06 #1
3 1642
KC
I'm new to JavaScript, but I think you can do something like this:

var obj=document.getElementById("changeme");
obj.style.position = 'relative';
obj.style.top = '-100px';
KC

applegree...@gmail.com wrote:
Hi, can someone please show me how to set the following conditions for
this DIV tag?

<div style="position:absolute;left:0px; top:0px;" id="changeme">HELLO
WORLD</div>

I want to be able to change the position to 'relative' and the top
position to negative '-100px' with javascript

Thank you! :))
Dec 11 '06 #2
KC You are exactly right!

But to make things a bit more easy you can put it in a function, like
so:

function changePosition(id,posType,x,y){
var obj = document.getElementById(id);

obj.style.position = posType;
obj.style.left = x;
obj.style.top = y;
}

Now you can use this function everytime you want to do a change like
this bij calling the function: example:

<div onclick="changePosition(this,'absolute', '-100px', '0px')">

this should work as well and might make things a bit more handy.

Yorian

KC wrote:
I'm new to JavaScript, but I think you can do something like this:

var obj=document.getElementById("changeme");
obj.style.position = 'relative';
obj.style.top = '-100px';
KC

applegree...@gmail.com wrote:
Hi, can someone please show me how to set the following conditions for
this DIV tag?

<div style="position:absolute;left:0px; top:0px;" id="changeme">HELLO
WORLD</div>

I want to be able to change the position to 'relative' and the top
position to negative '-100px' with javascript

Thank you! :))
Dec 11 '06 #3
Great stuff yorian!

extra credit style-setting function for over-achievers:

var myHelpers // Name space your code to avoid embarrassing collisions.
// You might want to pick a more unique namespace.
// com.yourSite is a pretty solid choice
// Don't always have to do this, but if you want others to use your
code, or to
// user others code it's something to think about.

myHelpers.setStyle = function( id, styles )
{
var style,
element = document.getElementById( id )

for ( style in styles ) // for each key in a { key: 'value' }
construct
{
if ( styles.hasOwnProperty( style ) // don't want to go running
up the prototype chain
{
element.style[ style ] = styles[ style ]
}
}
}

Use it like:

myHelpers.setStyle( 'someElement', { color: '#fff', backgroundColor:
'#454545' } )

Happy scripting!

Dec 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bayram guzer | last post by:
hi everybody, i have very strange error. i can not see some of the asp pages on my browser. when i look from view source, all the source is there but browser does not show anything, just an empty...
5
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
3
by: Ryh | last post by:
I have the following scritpt. It hides div layer when mouse is out of the div layer. Inside DIV I have IFRAME box. Unfortuantely it does not work in Mozilla or IE 5.5. It hides div when cursor is...
1
by: Oliver Hoehle | last post by:
Hello! This ist the source-code for an editable combobox implemented with HTML,CSS and Javascript. I have tested it with IE and Mozilla. But I don't know, if it will work in other browsers...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: Joseph | last post by:
The idea is to show only one of the <Baby_Div> while hiding all the others. At the moment all I have managed to do is to show each <Baby_Div> in turn as expected, but the problem is that once a...
2
by: MOHSEN KASHANI | last post by:
Hi, I am trying to hide some form elements in a form by default and show/hide depending on which radio button is clicked. This is what I have but it is not working: <head> <style> ..noshow {...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.