1

I have a JavaScript array that contains a set of strings. I want to display them in a HTML div element by line by line using j Query or JavaScript.

My code is up to now:

var data = data;
for (i = 1; i <= data.length; i++) {

  data[i] = data[i] + '<br />';

  $(target).html('<a>'+data[i]+'</a>');
}

My data is displayed in this moment right now.

Labelling MachinesLabels - Plastic, Metal, Foil etcLabels FabricLaboratories - MedicalLaboratories - TestingLaboratory Equipment & SuppliesLaboratory Equipment Services & Calibration

I want them displayed like this as links (inside tags):

Labelling Machines
Labels - Plastic, Metal, Foil etc
Labels Fabric
Laboratories - MedicalLaboratories - Testing
Laboratory Equipment & Supplies
Laboratory Equipment Services & Calibration

Thanks in advance

1
  • You need to use append method check my answer. Commented Sep 29, 2017 at 7:02

5 Answers 5

2

You should add the breaks outside of the link tags and use .html() only once, as it completely replaces the innerHTML of the given element, i.e.

str = "";
for (i = 1; i <= data.length; i++) {
    str += "<a>" + data[i] + "</a><br />";
}
$(target).html(str);

I would suggest another approach, to use innerHTML (javascript) or append (jquery) as another answer has already mentioned

for (i = 1; i <= data.length; i++) {
    target.innerHTML += "<a>" + data[i] + "</a><br />";
}
Sign up to request clarification or add additional context in comments.

2 Comments

html() method replaces the target html every iteration of the loop
@charlietfl that's why I used append method.
0

Your code is incomplete here.Not sure if you have declare variable i anywhere in code.Also you are starting to loop from 1st index

Instead of appending to DOM on every iteration,create a string and concat the value to it. Append it on completion of the iteration.

var data = data,
htmlString="";
for (var i = 0; i <= data.length; i++) {
htmlString+= data[i] + '<br />';
}
$(target).append(htmlString);

Comments

0

The cleanest way will be wrapped in a div. And you need to use .append() method to not override the initial data that is already added to the target.

var data = ["Hello", "World", "Lorem", "Ipsum", "More length"];
for (var i = 0; i < data.length; i++) {
  $("#result").append('<div><a href="#" class="link">' + data[i] + '</a></div>');
}
.link {
  color: #5ca5cc;
  margin-bottom: 10px;
  text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>

Clean and more simple code along with working demo.

Comments

0

Instead of a for/loop you could use ES6 in one line with map and a template literal.

$(target).html(arr.map(el => `<a>${el}</a><br/>`));

DEMO

Comments

0
var data = data;
var str = '';
for (var i = 1; i <= data.length; i++) {

  str += `<a>${data[i]}<br /></a>`;

}
$(target).html(str);

Try this.

1 Comment

This creates only one <a> with a list of items in it. Doesn't make sense

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.