jQuery CSS Position as a Number
Question:
How can we retrieve the CSS position of an element as a number instead of as a String so the position value can be used for further calculations?
Answer:
The jQuey function $(selector).css(propertyName) will return the string value for the specified CSS property. Then we can use parseFloat(theString) to parse the string to a float number. So we can combine the two functions and write parseFloat($(selector).css(propertyName)).
Lets use an example: Retrieve the numeric value for the CSS property left on the element with id helloWorld. The function call parseFloat($(”#helloWorld”).css(”left”)) will give us this value as a number.
You can also use parseInt(theString, radix) to pars the string into a integer number. The radix specifies the number base to use in the parsing. If you do not know witch base to use the most common one is 10 as it is our standard decimal number system. Use a radix of 6 if you want to parse a hexadecimal string and 2 if you have a string representing a binary number.