Setting CSS position using Javascript

How to set relative 50% 50% position using JS?

I tried:

document.getElementById("id").style.position = "relative 50% 50%";

but it don't seems to work...

Answers


You have to set them individually.

I made a variable to point to the style object, because we are modifying more than one property and because with() { ... } is considered harmful.

Also, I set the 50% to both properties because of right to left assignment, and because assignment of this string to the two properties isn't a problem (make sure you understand how this works, e.g. var a = b = [] will set a and b to the same Array object, often not desired).

var elementStyle = document.getElementById("id").style;

elementStyle.position = "relative";

elementStyle.top = elementStyle.left = "50%";

jsFiddle.


Need Your Help

Is it safe to use MD5 checksums to search for duplicate files across multiple hard drives?

python md5 checksum

I've been tasked with consolidating about 15 years of records from a laboratory, most of which is either student work or raw data. We're talking 100,000+ human-generated files.

How can I add a restriction to a complextype in XML (XSD) schema?

xml xsd schema restriction

Can anyone help me to add a restriction to this schema file (for OwnerEnglishName)? I know how to do it with a simpletype, while in a complextype I don't know how to do it. Can anyone help?