Calculate width of element using jQuery

Valip

How can I set the width of an input equal with 100% - width of an img?

My goal is to display both of them on the same line and make the input to take the full width of the remaining space.

This is what I got so far

HTML:

<div class="img-container">
  <img src="http://dreamatico.com/data_images/car/car-1.jpg"/>
</div>
<input class="img-url" placeholder="enter img url" />

CSS:

.img-container {
  width: 30px;
  height: 30px;
}
.img-container img {
  width: 30px;
  height: 30px;
}

jQuery:

$(document).ready(function(){
  var img_container_width = $('.img-container').width();
  $(".img-url").width('100%' - img_container_width);
});

jsfiddle: https://jsfiddle.net/bko38bt2/2/

Nenad Vracar

You can do this with just css using Flexbox

.img-container {
  display: flex;
}
.img-container img {
  width: 30px;
  height: 30px;
}

input {
  flex: 1;
}
<div class="img-container">
  <img src="http://dreamatico.com/data_images/car/car-1.jpg"/>
  <input class="img-url" placeholder="enter img url" />
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery - calculate element width through subtract pixel from window size

From Dev

Increase element width by percentage using jQuery

From Dev

Using jQuery function if element width is valid

From Dev

Calculate width of element - Angular Directive

From Dev

Changing the width of HTML div element automatically using jQuery

From Dev

Using jquery to calculate height of an element that contains another with a margin

From Dev

calculate element width based on first line of children

From Dev

Calculate element width before resize against width after resize

From Dev

Calculate element width before resize against width after resize

From Dev

Incorrect jQuery element width calculation

From Dev

jQuery width of element added in loop

From Dev

jQuery width (%) doesn't stop calculate on windowResize?

From Dev

Calculate width for each list item with jquery

From Dev

jquery calculate height and width and animate content behind it

From Dev

How to calculate width of text using javascript?

From Dev

How can I dynamically apply the "left" px to a div's width, within the same parent element, using jQuery

From Dev

How can I find the intended render width of an element using jQuery before it is rendered?

From Dev

Display the biggest width of element among other three elements inside a Div using Jquery

From Dev

Using jquery to determine screen width

From Dev

Toggle Width of a Div Using Jquery

From Dev

Toggle width of a container using jQuery

From Dev

Toggle Width of a Div Using Jquery

From Dev

How to find a parent element width in JavaScript or jQuery?

From Dev

Use JQuery each function to return element width

From Dev

Animate the width of a block element as it changes with jQuery?

From Dev

jQuery, set element width minus size in em

From Dev

Jquery horizontal pan with 100% width element

From Dev

jQuery: change element position depending on screen width

From Dev

Jquery :Automatically change the width equal to an element

Related Related

  1. 1

    jQuery - calculate element width through subtract pixel from window size

  2. 2

    Increase element width by percentage using jQuery

  3. 3

    Using jQuery function if element width is valid

  4. 4

    Calculate width of element - Angular Directive

  5. 5

    Changing the width of HTML div element automatically using jQuery

  6. 6

    Using jquery to calculate height of an element that contains another with a margin

  7. 7

    calculate element width based on first line of children

  8. 8

    Calculate element width before resize against width after resize

  9. 9

    Calculate element width before resize against width after resize

  10. 10

    Incorrect jQuery element width calculation

  11. 11

    jQuery width of element added in loop

  12. 12

    jQuery width (%) doesn't stop calculate on windowResize?

  13. 13

    Calculate width for each list item with jquery

  14. 14

    jquery calculate height and width and animate content behind it

  15. 15

    How to calculate width of text using javascript?

  16. 16

    How can I dynamically apply the "left" px to a div's width, within the same parent element, using jQuery

  17. 17

    How can I find the intended render width of an element using jQuery before it is rendered?

  18. 18

    Display the biggest width of element among other three elements inside a Div using Jquery

  19. 19

    Using jquery to determine screen width

  20. 20

    Toggle Width of a Div Using Jquery

  21. 21

    Toggle width of a container using jQuery

  22. 22

    Toggle Width of a Div Using Jquery

  23. 23

    How to find a parent element width in JavaScript or jQuery?

  24. 24

    Use JQuery each function to return element width

  25. 25

    Animate the width of a block element as it changes with jQuery?

  26. 26

    jQuery, set element width minus size in em

  27. 27

    Jquery horizontal pan with 100% width element

  28. 28

    jQuery: change element position depending on screen width

  29. 29

    Jquery :Automatically change the width equal to an element

HotTag

Archive