Pascal
Pascal

Reputation: 12855

Add hyperlink with trailing slash with jquery trashes the hyperlink closing tag

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

Answers (3)

arnolds
arnolds

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

Marc B
Marc B

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

Musa
Musa

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

Related Questions