Search code examples
javascripthref

Get a string in a variable to create a dynamic link in Javascript


What I am trying to accomplish is to have a map that has a few locations. Whenever users click on each location, a popup will emerge with some information. I am trying to create a dynamic link inside that popup.

Below is my code in Javascript

function parseDescription(message){
    var string=""

    for(var i in message){
        if (i=="CommunityPartner"){
            string+='<span style="font-weight:bold">'+i+'</span>'+": "+message[i]+'</a>'+"<br>"
        } else if (i=="WeitzCECPartner"){
            string+='<span style="font-weight:bold">'+i+'</span>'+": "+message[i]+'</a>'+"<br>"
        } else if (i=="PhoneNumber"){
            string+='<span style="font-weight:bold">'+i+'</span>'+": "+message[i]+'</a>'+"<br>"
        } else if (i=="Website"){
            var link = "http://www."+message[i];
            string+='<span style="font-weight:bold">'+i+'</span>'+": "+'<a href="{{link}}" >'+link+'</a>'+"<br>"
        }
        //string+='<span style="font-weight:bold">'+i+'</span>'+": "+message[i]+"<br>"
    }
    return string;
}

enter image description here

I keep getting this error. I think it's related to the value passed into "a href" :

Request Method: GET
Request URL:    http://127.0.0.1:8000/%7B%7Blink%7D%7D

Please help


Solution

  • Instead of using {{link}} in the string, you can try this:

    var link = "http://www." + message[i];
    string += '<span style="font-weight:bold">' + i + '</span>: <a href="' + link + '">' + link + '</a><br>';