Hendrix
Hendrix

Reputation: 179

Multiple line replace jQuery textarea

I have a question with the following script, apparently works well when a line of code, but if to replace several lines of code does not work :(

A line of code:

enter image description here

Multiple lines of code

enter image description here

When the result should be:

http://www.domain.com/myimages/123/123_small.jpg
http://www.domain.com/myimages/456/456_small.jpg
http://www.domain.com/myimages/789/789_small.jpg

JSFIDDLE:

http://jsfiddle.net/mxk6fLpg/

<h3>Enter:</h3>

<textarea value="" id="enter"></textarea>
<h3>Result:</h3>

<textarea class="disable" id="result"></textarea><br />
<input type="reset" value="Resetear" class="button reset" />
$('#enter').keyup(function () {
    var url = 'http://www.domain.com/myimages/' + $(this).val() + '/' + $(this).val() + '_small.jpg';
    var result = url;
    $('#result').html(result);
});

Upvotes: 3

Views: 1414

Answers (1)

Caner Akdeniz
Caner Akdeniz

Reputation: 1862

There might be better way to implement this but, you can split your string with line breaks and display each line break strings in new line. To accomplish this, here is your modified code:

$('#enter').keyup(function () {
    var eachLine = $(this).val().split('\n');
    var result = '';       
    for(var i=0;i<eachLine.length;i++){
        var url = 'http://www.domain.com/myimages/' + eachLine[i] + '/' + eachLine[i] + '_small.jpg';
        result = result + url + '\n';
    }        
    $('#result').html(result);
});

Here is the demo: http://jsfiddle.net/mxk6fLpg/2/

Upvotes: 2

Related Questions