1

I have below function:

$(".import-shipments").on("click", function(){
        var sendinger = $('#shipments').val().split("/\n/");

        for(var i = 0; i < sendinger.length; i++){
                    console.log(sendinger[i]); //This returns 3 lines
                    addRow(i,"#rows") //It only adds 1 line
        }

});

Which takes the values in my textarea, and count each value per line.

I then have below function, which should append a new "row", for each value:

  function addRow(id, element){

            var row = '<div class="row">'+
                      '<div>#'+id+'</div>'
                      '</div>';

                     $(element).append(row);

  }

The problem is, that above only appends one row, when it should append 3.

Please see this jsFiddle for an example on how above works.

What I want to do is, for each line in the text area, it should also run the addRow() function for each line.

1 Answer 1

3

You are using wrong argument in split function. You are mixing regex and string here. which returns only one element in seninger array. it should be:

 var sendinger = $('#shipments').val().split(/\n/); //USING Regex

or

 var sendinger = $('#shipments').val().split("\n"); //USING String

Working Demo

Sign up to request clarification or add additional context in comments.

3 Comments

or .split(/\n/)
was updating that only :)
Works like a charm! Thank you!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.