Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width of button Javascript without the "px"

I have some code that gets the width of a button, but it always returns the width of the button with "px" on the end. Is there an easy way (without creating a function that removes the last two characters) of getting the width without the "px" part, but still in pixels?

jsfiddle example

<button name="test" id="mybutton" onclick="alert(this.style.width);" style="width:200px;">Click me</button>

Thanks in advance

like image 534
user2704237 Avatar asked Dec 03 '22 21:12

user2704237


1 Answers

Hey a quick a simple answer for this is to use parseInt on the number

<button name="test" id="mybutton" onclick="alert(parseInt(this.style.width,10));" style="width:200px;">Click me</button>

See the fiddle here http://jsfiddle.net/XdADS/6/

like image 135
Dominic Green Avatar answered Dec 05 '22 11:12

Dominic Green