# JavaScript Computing minimum of two lengths in JavaScript

I need to set for a `div` a `margin-left` in JavaScript in such a way that its value if the minimum between two lengths in different units, e.g. `min(10pt,15vw)`.

There is `Math.min()` that we can use to compare numbers. Is there a similar function for lengths?

Alternatively, is there a way to convert length from one unit to another?

The minimum of two lengths with separate units can be calculated by creating a temporary hidden `div` and using the `offsetWidth` property to calculate the layout width of the element in pixels (followed by immediate removal of the `div`):

``````const minimum = (a, b) => {
const temp = document.createElement('div');
const size = [];

temp.style.border = 0;
temp.style.overflow = 'hidden';
temp.style.visibility = 'hidden';

document.body.appendChild(temp);

temp.style.width = a;
size[0] = temp.offsetWidth;
temp.style.width = b;
size[1] = temp.offsetWidth;

temp.parentNode.removeChild(temp);

return Math.min(size[0], size[1]) - size[0] ? b : a;
};

console.log('The minimum width is:', minimum('100px', '100%'));``````

Since 1vw reflects 1% of the viewport's width, you can convert it to pixels using window.innerWidth:

``````var 1vw = window.innerWidth * 0.01;
``````

and comparing that with 15 pixels:

``````Math.min(1vw * 15, 10);
``````

## Hidden characters causing JQuery function to fail

I am trying to understand what causes a simple jquery function to fail. It simply looks for the ID, replaces some content with other data. I have \$data1, \$data2, \$data3 as the replacement test data ...

I am trying to understand what causes a simple jquery function to fail. It simply looks for the ID, replaces some content with other data. I have \$data1, \$data2, \$data3 as the replacement test data ...

## fill an array with getElementById

I am trying to fill an array with numbers that I have in my HTML file by using getElementById. For the HTML, I am attempting to do this using: <div id="E1_INV_Funds0">1</div> <div id="...

I am trying to fill an array with numbers that I have in my HTML file by using getElementById. For the HTML, I am attempting to do this using: <div id="E1_INV_Funds0">1</div> <div id="...

1. fill array with random numbers
2. fill array with zeros
3. fill array with random numbers c++
4. fill array with zeros python
5. fill array with 0
6. fill array with random numbers python
7. fill array with zeros c++
8. fill array with random numbers javascript
9. fill array with zeros javascript
10. fill array with values
11. fill array with numbers java
12. fill array with same value python
13. fill array with numbers
14. fill array with user input java
15. fill array with values java
16. fill array with values javascript
17. fill array with value numpy
18. fill array with value c++
19. fill array with same value java
20. fill array with null c

## Remove multiple localStorage items

I have two 'localStorage' items, summaryForm and projectForm. Currently I use two statements to remove them, localStorage.removeItem("summaryForm"); localStorage.removeItem("projectForm"); Is it ...

I have two 'localStorage' items, summaryForm and projectForm. Currently I use two statements to remove them, localStorage.removeItem("summaryForm"); localStorage.removeItem("projectForm"); Is it ...

1. remove multiple items from localstorage

## keep filter after ag-grid update

I'm trying to keep a grid's filter after updating a row. When I click on a row in the grid, I open a dialog where I update the informations for this row, and then I look for the clicked row in the ...

I'm trying to keep a grid's filter after updating a row. When I click on a row in the grid, I open a dialog where I update the informations for this row, and then I look for the clicked row in the ...

1. keep rank after filter tableau
2. keep rows hidden after filter
3. primefaces datatable keep filter after update
1