Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  88] [ 7]  / answers: 1 / hits: 125960  / 14 Years ago, tue, march 8, 2011, 12:00:00

I have a textarea in my HTML. I need to get the padding numerical value in pixels as either integer or float. How can I get it using JavaScript? I am not using jQuery, so I'm looking for pure JavaScript solutions.


More From » html

 Answers
38

This will return the padding-left value:


window.getComputedStyle(txt, null).getPropertyValue('padding-left')

where txt is the reference to your TEXTAREA element.


The above works in all modern browsers and in IE9. However, it does not work in IE8 and below.


Live demo: http://jsfiddle.net/simevidas/yp6XX/


Further reading: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle




Btw, just for comparison, this is how you get the same job done using jQuery:


$(txt).css('padding-left')

The above does work in IE6-8.


[#93383] Sunday, March 6, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nestorjarettg

Total Points: 451
Total Questions: 108
Total Answers: 108

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;