I am trying to add a link into a paragraph that already has some text in it, but when I try this is just adds the text of the link element and not actual link. href or <a>
tag. Here is html
<div id="main_div"></div>
And here is Javascript
var temp_link = document.createElement("A");
temp_link.href = "";
temp_link.target = '_blank';
var par = document.createElement("P");
par.innerHTML = "some text: " + temp_link;
<!--I have also tried this line below -->
<!-- par.insertAdjacentHTML('beforeend', '<div><a>' + url +'</a></div>' -->);
document.getElementById("main_div").appendChild(par);
I have tried two things what is in the above javascript both what it would currently run and the line that is mented out, neither attach the link, they just add the text I have included JSFiddle.
How can I add it so that <a>
links?
I am trying to add a link into a paragraph that already has some text in it, but when I try this is just adds the text of the link element and not actual link. href or <a>
tag. Here is html
<div id="main_div"></div>
And here is Javascript
var temp_link = document.createElement("A");
temp_link.href = "http://test.";
temp_link.target = '_blank';
var par = document.createElement("P");
par.innerHTML = "some text: " + temp_link;
<!--I have also tried this line below -->
<!-- par.insertAdjacentHTML('beforeend', '<div><a>' + url +'</a></div>' -->);
document.getElementById("main_div").appendChild(par);
I have tried two things what is in the above javascript both what it would currently run and the line that is mented out, neither attach the link, they just add the text I have included JSFiddle.
How can I add it so that <a>
links?
-
Why have you tagged the question with
jQuery
when you're not using it? – Lee Taylor Commented Dec 8, 2015 at 2:20 - @LeeTaylor Im not using to it but an open to a solution that involves/ uses it – spen123 Commented Dec 8, 2015 at 2:21
-
You can use
$("#main_div").append(par);
to append the element. Also using$(*selector*).attr("attr name", "value")
is probably your best bet for adding attributes. – CalebB Commented Dec 8, 2015 at 2:23
3 Answers
Reset to default 6You can not mix innerHTML and createElement. You need to append the element to the paragraph.
var temp_link = document.createElement("a");
temp_link.href = "http://test.";
temp_link.target = '_blank';
temp_link.innerHTML = "link";
var par = document.createElement("p");
par.innerHTML = "some text: ";
par.appendChild(temp_link);
document.getElementById("main_div").appendChild(par);
<div id="main_div"></div>
or
var temp_link = document.createElement("a");
temp_link.href = "http://test.";
temp_link.target = '_blank';
temp_link.innerHTML = "link";
var text = document.createTextNode("some text: ");
var par = document.createElement("p");
par.appendChild(text);
par.appendChild(temp_link);
document.getElementById("main_div").appendChild(par);
<div id="main_div"></div>
use innerHTML for the link name and outerhtml to get the full html of a elelmt
var temp_link = document.createElement("A");
temp_link.href = "http://test.";
temp_link.target = '_blank';
temp_link.innerHTML ='click here';
var par = document.createElement("P");
par.innerHTML = "some text: " + temp_link.outerHTML;
document.getElementById("main_div").appendChild(par);
Something like this should get you started:
var $p = $("<p>").text("some text:");
$p.append(
$("<a>").attr("href", "http://test.")
.attr("target", "_blank")
.text("click me")
);
$("#main_div").append( $p);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="main_div"></div>