Reputation: 12855
I do this:
$(selector).append("<li><a href=" + url + ">" + title + "</a></li>");
Its rendered as:
</a><//a>
My main question is why is that?
My side question is it safe to just remove the last char if its a '/'
Upvotes: 0
Views: 81
Reputation: 788
The problem was with your quotes.
var url = 'http://stackoverflow.com',
title = 'Stackoverflow';
$('.example').append('<li><a href="' + url + '">' + title + '</a></li>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="example"></div>
Upvotes: 0
Reputation: 360682
You don't have any quotes around the generated href. A bare url may naturally contain a /a
or something which could be parsed as closing your <a>
tag.
$(selector).append("<li><a href=\"" + url + "\">" + title + "</a></li>");
^^-----------^^
Upvotes: 1
Reputation: 97672
You should really quote your href attribute value
$(selector).append("<li><a href=\"" + url + "\">" + title + "</a></li>");
the slash at the end is closing the <a/>
tag
Upvotes: 1