I am trying to dynamically create href links within Mapbox popups. When hovering over a location, I am returned an array, for example:
"Feature","properties":{"field_1":71,"city":"Gothenburg",
"course1":"EMM-Nano","course2":"EMHRPP","course3"
:"MFAMILY","course4":"NA","course5":"NA","course6":"NA",
"course7":"NA","course8":"NA","course9":"NA","course10":"NA",
"course11":"NA","course12":"NA","course13":"NA","course14":"NA",
"course15":"NA","course16":"NA","lon":11.97456,"lat":57.70887,
"website1":"http://www.emm-nano.org/",
"website2":"http://ws1.roehampton.ac.uk/admissions/erasmusmundus/hrp/index.html",
"website3":"http://www.mfamily.eu/",
"website4":"NA","website5":"NA","website6":"NA",
"website7":"NA","website8":"NA","website9":"NA",
"website10":"NA","website11":"NA","website12":"NA",
"website13":"NA","website14":"NA","website15":"NA",
"website16":"NA","code":"Gothenburg","title":"Gothenburg"
As you can see, the urls which will serve as href are strings within the array. Each course is connected to a website so that course1 should be linked to website1, course2 should be linked to website2 and so on. At this point, my popups only display courses and links separately for example:
with the following code to remove the NA and other unwanted values:
var feature = features[0];
function htmlFromProps(props) {
var html = "";
var i = 0;
for (p in props) {
if (props[p] && props[p] != "null" && props[p] != "NA") {
html += "<div>" + (i === 0 ? "<strong>" : "");
html += props[p];
html += (i++ === 0 ? "</strong>" : "") + "</div>\n";
}
}
return html;
}
//delete unnecessary elements*
delete feature.properties["field_1"];
delete feature.properties["lon"];
delete feature.properties["lat"];
delete feature.properties["code"];
delete feature.properties["title"];
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(feature.geometry.coordinates)
.setHTML(htmlFromProps(feature.properties))
.addTo(map);
}
This works, but the links are not attached to the course names. I am interested in only displaying the course names themselves that include hrefs Is there a programmatic way to go about this in JS without using CSS? I have researched several examples as this one but it is not applicable to the structure as it is now. Any advice on how to go about this problem would be appreciated.
