1

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.

1 Answer 1

2

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">

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. It helps alot, just one question though when applying "window.innerwidth" it's adding a scroll bar as the control width is to the size of window. Is their a way to correct this, maybe assigning width equal to some other control?
Yes.. if you want to make the scroll bar disappear, just subtract some small amount from the windowWidth variable. For example, try this: var windowWidth = window.innerWidth - 100. Adjust the 100 according to your liking and until the scroll bar has disappeared. There are also many more methods to make it disappear.
@ishaanGupta Also, you're welcome. Please mark the answer as "accepted" if it worked for you! Thanks!
Code works perfectly, just a small addition i did according to my requirement, was instead of setting width equal to window, I assigned width of another textbox "var windowWidth = $("#input2").width()" to it so both of them are in perfect sync while resizing the window. Still Thank you very much for guiding me through the process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.