0

I am trying to build some list elements using jQuery append(...) but I do not seem to get the syntax correct. I have come part way (with the correct output), but I am sure that there is a better way to use the function that does not require my building HTML directly from strings.

My desired result...

<a href='...;'>
  <div>Wigan</div>
  <div style="font-size: 8px">Wigan, null, NaN</div>
</a>

My JavaScript...

var result = $("<a>", {href: "..."});
result.append("<div>"+ results[index].Region + "</div>");
result.append("<div style = \" font-size: 8px\">" + results[index].District + ", " + results[index].Postcode + ", " + +results[index].CountryCode + "</div>");

What I have above works, but how do I group the append calls and the content to build this up without simply creating the strings as I do above?

Thanks.

3
  • Have you looked into something like mustache to template out repeated elements like this? Commented May 6, 2015 at 15:10
  • @BrianMuenzenmeyer: No, until now I had never heard of it. If possible, I would prefer to avoid adding new frameworks at this time though as I know that I can achieve what I need with jQuery even if I end up doing it the way I have shown above. Thanks though. Commented May 6, 2015 at 15:13
  • Understandable. template frameworks give you a lot of power - but what TrueBlueAussie describes below would work in a bind :) Commented May 6, 2015 at 15:17

2 Answers 2

1

If you do not want to use a template addin (like moustache) here is a simple trick:

Put your template in a dummy script block with an unknown type

<script type="text/template" id="template">
        <a href='...;'><div>{{name}}</div><div style="font-size: 8px">{{name}}{{someother}}</div></a>
</script>

and use as if it was a plain HTML string. Use replace to insert your values into any placeholders:

var html = $('#template').html().replace(/{{name}}/g, myname).replace(/{{someother}}/g, someother)

The result is ready to append/insert etc into the DOM.

Notes:

  • I used the moustache-style markers for this example, but anything unique will do to match.
  • This makes editing your template WYSIWYG/obvious so maintenance is easier.
Sign up to request clarification or add additional context in comments.

3 Comments

I like what you have done there, but for consistency with the way the application is currently built I have used @Veo's solution instead. Thanks.
@Martin Robins: Your call. You asked for a better way (and there are several) :)
Yeah that's the problem with these types of questions. Lots of ways to skin a cat and it boils down to personal taste.
0

Something like:

result.append($('<div>').html(results[index].Region), $('<div>',{ style:' ...' }));

or

$('<div>').html(results[index].Region).appendTo(result)

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.