473,386 Members | 1,842 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,386 software developers and data experts.

[SOLVED] how to document.getElementById().style.padding-left ?

1
I need to change the style of an element changing its padding-left property (got it in firefox but IE is a different battle..). I can use document.getElementById().style.XX
to everything i need, except with padding-left, as it is separated by a ' - ' and it is not correct to use it as it was supposed to:

document.getElementById('el').style.padding-left = '5px'

will throw 'invalid assignment left-hand side', as expected..

Any help please?

[SOLVED]

Apparently the property turns into paddingLeft (or paddingTop, paddingBottom, paddingRight) so it can be used as expected:

document.getElementById('el').style.paddingLeft = '5px'
document.getElementById('el').style.paddingTop = '5px'
document.getElementById('el').style.paddingRight = '5px'
document.getElementById('el').style.paddingBottom = '5px'
Mar 25 '08 #1
2 13856
hsriat
1,654 Expert 1GB
I need to change the style of an element changing its padding-left property (got it in firefox but IE is a different battle..). I can use document.getElementById().style.XX
to everything i need, except with padding-left, as it is separated by a ' - ' and it is not correct to use it as it was supposed to:

document.getElementById('el').style.padding-left = '5px'

will throw 'invalid assignment left-hand side', as expected..

Any help please?
For that use paddingLeft.

In general, anything of style containing a hyphen ( - ) will work for JavaScript when hyphen is removed and first letter of the second word is made capital.

eg.
background-image -> backgroundImage
border-top -> borderTop


[EDIT]
Glad to know you solved it.
Mar 25 '08 #2
mrhoo
428 256MB
Converting between css syntax('border-top-width') and javascript property names ('borderTopWidth') is a common webpage chore.
The values you are working with are always strings, so a String method to do the conversion may be appropriate:

Expand|Select|Wrap|Line Numbers
  1.  
  2. String.prototype.dash= function(boo){
  3.     var s= this;
  4.     if(/^[A-Z]+$/.test(s) || /\-/.test(s)) s = s.toLowerCase();
  5.     if(boo=== true ){
  6.         if(/[a-z][A-Z]/.test(s)){
  7.             s= s.replace(/[A-Z]/g, function(w){
  8.                 return "-" + w.toLowerCase();
  9.             })
  10.         }
  11.     }
  12.     else if(/\-/.test(s)){
  13.         s= s.replace(/\-[a-z]/g, function(w){
  14.             return w.charAt(1).toUpperCase() + w.substring(2);
  15.         })
  16.     }
  17.     return s;
  18. }
  19.  
// pass the argument 'true' for css (hyphen-ated) , leave it out or pass 'false' to return camelCase.
Mar 25 '08 #3

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

Similar topics

2
by: Chris | last post by:
Hi, I'm having a problem utilizing the DOM. I tried using my own code, but it wasn't working, so I tried to use code straight from another web site and it verified that the problem may not be...
1
by: Jaap | last post by:
Hi, For a hobby site, I found this script and I'd like it to work the other way around. The content has to show and the visitor has to click to hide it. Could someone help me please? I'm a total...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
4
by: Ghyslaine Crespe | last post by:
Hello, In my script, the line document.getElementById(id).style.background-position = "-200px -500px"; fails ! So, how can I change the background-position value ?
5
by: s_m_b | last post by:
function saveState() { document.write (" | <a href = 'myhomepage.asp? view=mhp&amp;action=save&amp;pb="); >> document.write (document.phone.style.display.value); document.write ("'>save view</a>"); };...
7
by: PaulB | last post by:
Good Morning everybody, I'm trying to adapt a tutorial script that will handle the behaviour of an "Expanding/Contracting" site-navigation menu. The code that seems to handle the expansion and...
6
by: therig | last post by:
I'm having issues, I've spent many hours searching and I'm a noob at javascript, any help will be greatly appreciated. I keep getting the following error: Error: document.forms.sec11_A has no...
13
by: RommelTJ | last post by:
Hi, My website (http://www.justiceinmexico.org/indextest.php) looks good in Firefox, but horrible in IE, and I think it's because of an error in the javascript of a free web ticker I got off the...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.