Soo, Im working on an html-editor and setting vh values to div's. But when I try to use $(id).height();
or $(id).css("height");
it is always returning px value and not the vh. This should not be a problem, but I was planing on making is to a user could choose if he wished to use px or vh.
So is it another way to get jquery(or js) to tell me what type of height value that is set?
My question is not about converting data from px to %, but if it is possible to figure out what type of messurement the height is set as in css. In this case if it was defined as px or vh
Asked
Active
Viewed 2,974 times
0

Jon Vollar
- 19
- 2
- 6
-
Possible duplicate of [How can I convert px to vw in JavaScript?](http://stackoverflow.com/questions/28295072/how-can-i-convert-px-to-vw-in-javascript) – Vicky Gonsalves Jan 22 '16 at 13:22
-
1@Azim Why `.65`..? That won't work, will it? – somethinghere Jan 22 '16 at 13:22
-
Sorry. That was completely my fault. @somethinghere – Ibrahim Khan Jan 22 '16 at 13:31
-
_“setting vh values to div's”_ – how exactly are you doing that - also via `.height(…)` or `.css("height", …)`? Those set inline styles on the element, meaning you should be able to read the set value back via `$(id)[0].style.height` (`[0]` is needed here to de-reference the jQuery object, so that you get a reference to the “pure” HTML element object.) – CBroe Jan 22 '16 at 13:31
-
Well, Im setting the values first time using an input field with an on change event. This is setting the ).css("height", "100vh"); , at the same time it si executing an ajax call that is saving the value to a css file. So when I'm opening the html document for editing the next time im just including the css file. I could, and possible haftu add a seperate setting for the unit with the ajax call. But that also means I have to execute a extra ajax call everytime Im selecting the element, or adding a data-unit filed to the dom element... – Jon Vollar Jan 22 '16 at 13:42
-
I was hoping for a "cleaner" solution. – Jon Vollar Jan 22 '16 at 13:43
1 Answers
1
You will always get height or width in pixels. You will have to manually convert it to vh
or vw
value
Here is previous question: https://stackoverflow.com/a/28295133/1548301
1px = (100 / document.documentElement.clientWidth)vw
e.g. — If your viewport was 500px wide (equals by definition to 100vw) then1px = (100 / 500) = 0.2vw

Community
- 1
- 1

Vicky Gonsalves
- 11,593
- 2
- 37
- 58
-
I canda figured that out, but is there a way for js/jquery to tell me if im using vh and not px? So I know when to convert the value and when not to? As I said im using it as an editor, so when im clicking on different elements the js needs to figure out if what I just clicked is using vh or px – Jon Vollar Jan 22 '16 at 13:22
-
Well, that would be fine if I was planing to never open the "document" for editing ever again. Now I know i could save it seperatly in the db as some sort of setting to the document. But I was hoping I did not need to to so. I was hoping that all I needed was the css document that is beeing genereted. – Jon Vollar Jan 22 '16 at 13:29