3

I am using textarea to take user input. and want to read line by line. But it's not displaying anything I want to make a comma seperated list of the text in different lines

JS:

$('input[type=button]').click( function() {
    string = document.getElementById("hi").val();
    alert(string); 
    var html="";
    var lines = $('#id').val().split('\n');
    for(var i = 0;i < lines.length;i++){
        //code here using lines[i] which will give you each line
        html+=lines[i];
        html+=",";
    }
    $("#inthis").html(string);
});

HTML:

<textarea id="hi" name="Text1" cols="40" rows="5" placeholder="enter one wdg in one line" ></textarea>

<input type="button" value="test" />
<div id="inthis"></div>

Here is the jsfiddle:

http://jsfiddle.net/pUeue/1077/

8 Answers 8

5

Here's updated js...

Demo Fiddle

$('input[type=button]').click(function () {
    var html = "";
    var lines = $('#hi').val().split('\n');
    for (var i = 0; i < lines.length; i++) {
        //code here using lines[i] which will give you each line
        html += lines[i];
        html += ",";
    }
    html = html.substring(0,html.length-1);
    $("#inthis").html(html);
});
1

Firstly you have a confused mix of native javascript and jQuery code in your example. A native DOM element has no val() method for example, that's jQuery. Secondly, you could massively simplify your code by just using split() and join(','). Try this:

$('input[type=button]').click( function() {
    var string = $("#hi").val().split('\n').join(',');
    $("#inthis").html(string);
});

Example fiddle

0

Change:

string = document.getElementById("hi").val();

TO

string = document.getElementById("hi").value;

jsfiddle

0

http://jsfiddle.net/pUeue/1085/

$('input[type=button]').click( function() {
    string = $(document.getElementById("hi")).val();
    alert(string); 
    var htmlD="";
    var lines = string.split('\n');
    for(var i = 0;i < lines.length;i++){
    //code here using lines[i] which will give you each line
    htmlD+=lines[i];
    htmlD+=",";
}
    $("#inthis").html(htmlD);
});
0

I have corrected your code here: http://jsfiddle.net/pUeue/1080/.

$('input[type=button]').click( function() {
    string = $("#hi").val();
    alert(string); 
    var html="";
    var lines = $('#hi').val().split('\n');
    for(var i = 0;i < lines.length;i++) {
        //code here using lines[i] which will give you each line
        html+=lines[i];
        html+=",";
    }
    $("#inthis").html(html);
});
0

try this

$('input[type=button]').click( function() {
    var lines =  $('#hi').val().split(/\n/);
    $("#inthis").html(lines.join(","));
});
0

there is mistake in second line u have used jQuery val instead of value so try below:-

string = document.getElementById("hi").value;

and need to put the below line before you load the string in div.:-

html = html.substring(0,html.length-1);
0

.val() is a Jquery function, you will have to convert the DOM element to jquery obj or use .value

$('input[type=button]').click(function () {
  string = $(document.getElementById("hi")).val();
  string = string.replace("\n", ",")
  $("#inthis").html(string);
});

http://jsfiddle.net/pUeue/1090/

2
  • Thanks, but the fiddle doesnt show the comma seperated list.. Well, probably .split('\n') isnt used
    – ALBI
    Oct 17, 2013 at 9:31
  • oh yea, for comma you can just replace the \n with ',' without using split and join! I have updated the answer.
    – user2587132
    Oct 17, 2013 at 9:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.