ishaan Gupta
ishaan Gupta

Reputation: 308

Dynamically resize width of textbox with screen size using javascript

I have a textbox(input control) which I want to resize as per screen size using javascript. Is their a way to do it. Thanks for the help.

I have tried giving static width using document.getElementById, but not able to resize it dynamically as per webpage size.

Upvotes: 1

Views: 797

Answers (1)

Devashish
Devashish

Reputation: 1290

This code will set the width of the input equal to the width of the screen (dynamic and on resize):

$(document).ready(function() {
      var windowWidth = window.innerWidth
      $('#myInput').width(windowWidth)
      $(window).resize(function() {
        windowWidth = window.innerWidth
        $('#myInput').width(windowWidth)
      })
    })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="myInput">

Upvotes: 2

Related Questions