Reputation:
I am working on a project where you can insert a link. You can enter the link url and text. I used prompt to collect that information. I coded this:
document.getElementById(ev.target.id).innerHTML = y + "<a href=" + linkurl + " > + linktext + </a>";
When I use the code, The link appears as: + linktext +
.
Can I make the link text say the text that I prompted?
Upvotes: 1
Views: 251
Reputation: 6171
You missed double quote " + linktext + "
document.getElementById(ev.target.id).innerHTML = y + "<a href=" + linkurl + " > " + linktext + " </a>";
Upvotes: 1
Reputation: 3654
document.getElementById(ev.target.id).innerHTML = y + "" + linktext;
Upvotes: 0
Reputation: 943569
If you want to use a variable, don't put it inside a string literal. Include quotes around each piece of the literal string.
document.getElementById(ev.target.id).innerHTML = y + "<a href=" + linkurl + " > " + linktext + " </a>";
That said, piecing together HTML by mashing together strings can lead to rather confusing code, as you've discovered. Try using DOM methods instead.
var element = ev.target;
element.innerHTML = ""; // Erase existing content
element.appendChild(document.createTextNode(y));
var link = document.createElement("a");
link.href = linkurl;
link.appendChild(document.createTexTNode(linktext));
element.appendChild(link);
It is more verbose, but less error prone.
Upvotes: 1