0

I want to change a input text size when I change its value.

I have this javascript:

jQuery(document).ready(function ($) {

    function resizeInput() {
        $(this).attr('size', $(this).val().length);
    }

    $('input[type="text"]')
        // event handler
        .keyup(resizeInput)
        // resize on page load
        .each(resizeInput);

    document.getElementById("uploadBtn").onchange = function ()
    {
        document.getElementById("uploadFile").value = this.value;
        $("#uploadFile").attr('size', $("#uploadFile").val().length);
    };
});

First, I have test $('input[type="text"]') with resizeInput function. But it doesn't work.

Next, I have added that function to document.getElementById("uploadBtn").onchange = function () but it also doesn't work.

I want to resize input field uploadFile when I do this: document.getElementById("uploadFile").value = this.value;

How can I auto-scale an input size when I assign it a new string? Now it is autoscaling but it changes input's size with string characters count, and this is not what do I want to do.

I have used this SO answer as inspiration.

I have added this JSFiddle but I'm getting an error.

Community
  • 1
  • 1
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 2
    insteaf of `$(this).attr('size', $(this).val().length);` try `$(this).css('font-size', $(this).val().length);` – putvande Apr 01 '16 at 10:03
  • `$(this).attr('font-size', $(this).val().length);` – a45b Apr 01 '16 at 10:04
  • In your resizeInput(), can you console.log($(this)); ? Because I don't think you have the input at hand.. – Repo Apr 01 '16 at 10:15

1 Answers1

0

Try this: change size value as per length of value entered.

$(function(){
  $("input[type='text']").keyup(function(){
     var value = $(this).val();
     $(this).attr("size",value.length);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
Actually your code should work. You have define javascript function inside document.ready, just move it outside or make it jquery function.
jQuery(document).ready(function ($) {
  $("input[type='text']").keyup(resizeInput);
});
function resizeInput() {
    $(this).attr('size', $(this).val().length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
  • Thanks but I want to resize input field `uploadFile` when I do this: `document.getElementById("uploadFile").value = this.value;` – VansFannel Apr 01 '16 at 10:11
  • see my updated answer, you have to keep you resizeInput javascript function outside the document.ready. try this. – Bhushan Kawadkar Apr 01 '16 at 10:12
  • And now $(this) will actually be the input =) – Repo Apr 01 '16 at 10:19
  • I have added this https://jsfiddle.net/VansFannel/mv16t2as/1/ but it doesn't work. I', getting a POST error. – VansFannel Apr 01 '16 at 10:40
  • You have not added jquery library in jsfiddle. See [this](https://jsfiddle.net/kawadkarbk31/mv16t2as/3/), I have added jquery lib in External Resources (on left side menu) – Bhushan Kawadkar Apr 01 '16 at 10:48