Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When a button is pressed the Student name is added to a favourites box

function getFavourite()
{

    name = "Student 1, " 


    $("#output").val($("#output").val() + ' ' + name);
    saveFavourites();
    loadFavourites();
}



I'm trying to get the student to be a hyperlink that links back to the student's page

This is what I've been trying but with no luck

name = '<a href=' + window.location.href + '>'  + 'Student 1, ' + '/a>'
Posted

1 solution

You can try this:
JavaScript
var a = document.createElement('a');
var linkText = document.createTextNode("Text for your student");
a.appendChild(linkText);
a.title = "Text for your student";
a.href = "your url";

//you can change this to append anywhere
document.body.appendChild(a);


Here is how you can append your div with your new element:
JavaScript
function getFavourite()
{ 
   var name = document.createElement('a');
   var linkText = document.createTextNode("Student 1, ");
   name.appendChild(linkText);
   name.title = "Student 1, ";
   name.href = "your url for your student";
 
   //here you are appending your div or whatever element with a newly created 'a' element.
   $("#output").append(name);
   saveFavourites();
   loadFavourites();
}
 
Share this answer
 
v5
Comments
Member 10812199 12-May-14 9:26am    
Thanks for the responce, using the code below It displays only the url of the page but not as
a link. Do you have any ideas how I could fix it?

function getFavourite()
{

var name = document.createElement('a');
var linkText = document.createTextNode("Student 1, ");
name.appendChild(linkText);
name.title = "Student 1, ";
name.href = "";


$("#output").val($("#output").val() + ' ' + name);
saveFavourites();
loadFavourites();
}
norbitrial 14-May-14 10:50am    
It should work if you append a div with variable name. It is an 'a' element and if you concatenate with val(), it'll be casted as a string. That's why it is shown as a text.
I've improved my solution, you can check it, probably it'll work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900