473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[SOLVED] how to document.getEle mentById().styl e.padding-left ?

1 New Member
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.getEle mentById().styl e.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.getEle mentById('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.getEle mentById('el'). style.paddingLe ft = '5px'
document.getEle mentById('el'). style.paddingTo p = '5px'
document.getEle mentById('el'). style.paddingRi ght = '5px'
document.getEle mentById('el'). style.paddingBo ttom = '5px'
Mar 25 '08 #1
2 13906
hsriat
1,654 Recognized Expert Top Contributor
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.getEle mentById().styl e.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.getEle mentById('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 Contributor
Converting between css syntax('border-top-width') and javascript property names ('borderTopWidt h') 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
22234
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 mine. I am using IE 6.0.28 (sp5) on a Windows 2000 Server. I am very new to DOM so I'm not ruling out that I'm doing something wrong. However, I am posting the code that I got from another site because I did notice that the examples were using...
1
1200
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 Java-newbie... TIA Jaap Here's the code:
12
3945
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 both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById('weblogs').style.visibility='hidden';
4
5485
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) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
4
26600
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
1751
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>"); }; creates an error at this (>) line.
7
5997
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 contraction follows at the bottom of my message here. This following line is a big part of my hang-up: document.getElementById('m1').style.display='none';
6
3379
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 properties with this script: function checkform(btntype) {
13
4967
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 internet. When I run Firebug on it, it says: document.getElementById("TICKER") has no properties TICKER_CONTENT = document.getElementById("TICKER").innerHTML; Here is the complete script:
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10187
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5446
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.