0

I can show orderB.id as table data. I want to use this value in the href, but I am struggling with syntax.

        var output = results.reduce(function (orderA, orderB){
            var row = "<tr>";
            row += "<td>" + orderB.name + "</td>";
            row += "<td>" + orderB.type + "</td>";
            row += "<td>" + orderB.id + "</td>";
            row += "<td><a href='/update/' + orderB.id + >Update</a></td>";
            return orderA + row;
        },"")

I have tried:

            row += "<td><a href='/update/' + orderB.id + >Update</a></td>";
            row += "<td><a href='/update/' + 'orderB.id' + >Update</a></td>";
            row += "<td><a href='/update/orderB.id' + >Update</a></td>";

These output:

  1. /update/
  2. /update/
  3. /update/orderB.id

I want for example: /update/3

2
  • 2
    All of those lines are literal strings because they start with " and end with" with no break in between. End the literal string, insert the variable, and continue with the rest: "<td><a href='/update/" + orderB.id + "'>Update</a></td>"; Your editor should make it pretty clear when you're writing a string and when you're using a variable. Commented May 1, 2018 at 1:51
  • @CertainPerformance thank you this worked a charm Commented May 1, 2018 at 1:53

2 Answers 2

2

use template literals :

row += `<td><a href="/update/${orderB.id}">Update</a></td>`;

or simply concat your variables with the htmml string like :

row += '<td><a href="/update/' + orderB.id + '">Update</a></td>';
Sign up to request clarification or add additional context in comments.

Comments

0

As has been suggested, template literals are a good way to go, but you have to be careful about just sticking strings together to create HTML -- if there are characters like <, >, or & there can be problems. I'd use Lodash and modify the answer given by another poster like this:

row += `<td><a href="/update/${_.escape(orderB.id)}">Update</a></td>`;

Comments

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.