Jquery – convert textbox to multiline with jquery

jquerymultilinetextbox

I have a single line textbox.

I want to with jquery convert it to multiline but control how many lines that are added to it. I want to also be able to limit the number of characters on each line.

Any ideas how I can do this with jquery?

EDIT:

Yes what I meant by textbox was <input type="text">

EG. <input type="text" value="" name="txtTextBox1" id="xtTextBox1">

Best Answer

Considering you have following HTML:

<input type="text" class="myclasses" style="color: #123123" value="akira"/> 

then using following snippet:

(function($) {
    $.fn.changeToTextArea = function(rows, columns) {
        var attrs = {};
        var text = "";

        $.each(this[0].attributes, function(idx, attr) {
            attrs[attr.nodeName] = attr.nodeValue;
            if(attr.nodeName == "value") {
              text = attr.nodeValue;
              }
            attrs["rows"] = rows;
            attrs["cols"] = columns;
        });

        this.replaceWith(function() {
            return $("<textarea/>", attrs).append($(this).contents()).html(text);
        });
    }
})(jQuery);

You should call it with

$("input").changeToTextArea(7, 25);

(function($) {
    $.fn.changeToTextArea = function(rows, columns) {
        var attrs = {};
        var text = "";
      
        $.each(this[0].attributes, function(idx, attr) {
            attrs[attr.nodeName] = attr.nodeValue;
            if(attr.nodeName == "value") {
              text = attr.nodeValue;
              }
            attrs["rows"] = rows;
            attrs["cols"] = columns;
        });

        this.replaceWith(function() {
            return $("<textarea/>", attrs).append($(this).contents()).html(text);
        });
    }
})(jQuery);


$("input").changeToTextArea(7, 25);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="text" class="xyzxterms" style="color: #123131" value="akira"/>