dtgee
dtgee

Reputation: 1272

Combining text and hyperlink in a string?

I'm still completely new to HTML, Javascript, and jQuery, and I've been trying out how to do this for a really long time now, all answers I've been looking at while searching gave me inadequate or inaccurate results. Or maybe I'm just bad at googling.

Anyways, what I want to do is output to the website "text text link", where link is a href link and text is just regular text. What I have is

jQuery

$('#text').html("text text ");
$('#link').html('link');
$('#link').attr('href', 'http://something.com/');

HTML

<div id = "text"><a id = "link"></a></div>

My problem is that inside the HTML, <a id = "link"> is not being detected.

EDIT: prepend() prepends to my string more than once, how do I stop this from happening?

Upvotes: 0

Views: 411

Answers (1)

j08691
j08691

Reputation: 207861

Try

$('#text').prepend("text text ");
$('#link').html('link');
$('#link').attr('href', 'http://something.com/');

jsFiddle example

In your example, the first .html() function is overwriting the contents of the div, removing the link.

Upvotes: 3

Related Questions