monda
monda

Reputation: 3915

Jquery : Put line break (press enter button) on entering certain character in text area

 $('.mydesc').keyup(function () {
 var len = $(this).val().length;
if(len == 63){
    //put line break here 
}           
 });

on reaching 63 characters including space the cursor should b moved to next line (effect when i get enter button)

how can i do it in jquery ?

Upvotes: 2

Views: 406

Answers (3)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

do something like this:

if(len == 63){
    $(this).val($(this).val() + "\n");
}   

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93551

You probably want something like this:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/9bKbQ/5/

$('.mydesc').keyup(function () {
    var $this = $(this);
    var val = $this.val();
    var lines = val.split('\n');
    var result = [];
    for (var i = 0; i < lines.length; i++) {
        var line = lines[i];
        while (line.length > 5) {
            result.push(line.substring(0, 5));
            line = line.slice(5);
        }
        result.push(line);
    }
    $this.val(result.join('\n'));
});

Just change the 5's to the length you want.

Upvotes: 0

Felix
Felix

Reputation: 38102

You can add line break using \n when reach 63 characters:

$('.mydesc').keyup(function () {
    var len = $(this).val().length;
    if (len == 63) {
        this.value += '\n';
    }
});

Fiddle Demo

Upvotes: 1

Related Questions