How to Rewrite an onclick() Function in my Javascript to Call a Function












2















I created a map using Leaflet with clusters. I wanted to add a popup when you click the cluster and it will show a popup list of names of the markers inside the cluster. I found a code and it does exactly what I’m looking for: a list popup showing the names of the markers inside the cluster and when you click on the different names on the popup list the related marker popup appears. But when I added the code to my project, I get the error, “reference error onclick is not defined” and the function won't run. I research this error and I found it’s not good practice to use the onclick function in my javascript/HTML in the way the code is written. My question is, how do I rewrite the script to work the way the example I’m using. Here’s a link to the example I’m using to show what I’m trying to accomplish (and this example works when I download it and run it…no error) - http://www.digital-geography.com/working-with-clusters-in-leaflet-increasing-useability/. And I also included my script. Any help on how to make this work will greatly be appreciated. Or, I’m open to achieve this in a different way. Thanks in advance!



function openPopUp(id, clusterId){
map.closePopup(); //which will close all popups
map.eachLayer(function(layer){ //iterate over map layer
if (layer._leaflet_id == clusterId){ // if layer is markerCluster
layer.spiderfy(); //spiederfies our cluster
}
});
map.eachLayer(function(layer){//iterate over map rather than clusters
if (layer._leaflet_id == id){// if layer is marker
layer.openPopup();
}
});

markers.on('clusterclick', function(a){
if(a.layer._zoom == 6){
var myText = '<ul>';
for (feat in a.layer._markers){
myText += '<li><u onclick="openPopUp"(' + a.layer._markers[feat]._leaflet_id + ',' + a.layer._leaflet_id + ')>' + a.layer._markers[feat].feature.properties['cityName2'] + ',' + a.layer._markers[feat].feature.properties['cityName2'] + '</u></li>';
}
myText += '</u>';
var popup = L.popup().setLatLng([a.layer._cLatLng.lat,a.layer._cLatLng.lng]).setContent(myText).openOn(map);

}
})









share|improve this question























  • "reference error onclick is not defined" Is that really the exact error text? Sounds very strange

    – CertainPerformance
    Nov 13 '18 at 1:26













  • Sorry, it say "ReferenceError: openPopUp is not defined[Learn More]"

    – TonyT
    Nov 13 '18 at 1:28
















2















I created a map using Leaflet with clusters. I wanted to add a popup when you click the cluster and it will show a popup list of names of the markers inside the cluster. I found a code and it does exactly what I’m looking for: a list popup showing the names of the markers inside the cluster and when you click on the different names on the popup list the related marker popup appears. But when I added the code to my project, I get the error, “reference error onclick is not defined” and the function won't run. I research this error and I found it’s not good practice to use the onclick function in my javascript/HTML in the way the code is written. My question is, how do I rewrite the script to work the way the example I’m using. Here’s a link to the example I’m using to show what I’m trying to accomplish (and this example works when I download it and run it…no error) - http://www.digital-geography.com/working-with-clusters-in-leaflet-increasing-useability/. And I also included my script. Any help on how to make this work will greatly be appreciated. Or, I’m open to achieve this in a different way. Thanks in advance!



function openPopUp(id, clusterId){
map.closePopup(); //which will close all popups
map.eachLayer(function(layer){ //iterate over map layer
if (layer._leaflet_id == clusterId){ // if layer is markerCluster
layer.spiderfy(); //spiederfies our cluster
}
});
map.eachLayer(function(layer){//iterate over map rather than clusters
if (layer._leaflet_id == id){// if layer is marker
layer.openPopup();
}
});

markers.on('clusterclick', function(a){
if(a.layer._zoom == 6){
var myText = '<ul>';
for (feat in a.layer._markers){
myText += '<li><u onclick="openPopUp"(' + a.layer._markers[feat]._leaflet_id + ',' + a.layer._leaflet_id + ')>' + a.layer._markers[feat].feature.properties['cityName2'] + ',' + a.layer._markers[feat].feature.properties['cityName2'] + '</u></li>';
}
myText += '</u>';
var popup = L.popup().setLatLng([a.layer._cLatLng.lat,a.layer._cLatLng.lng]).setContent(myText).openOn(map);

}
})









share|improve this question























  • "reference error onclick is not defined" Is that really the exact error text? Sounds very strange

    – CertainPerformance
    Nov 13 '18 at 1:26













  • Sorry, it say "ReferenceError: openPopUp is not defined[Learn More]"

    – TonyT
    Nov 13 '18 at 1:28














2












2








2








I created a map using Leaflet with clusters. I wanted to add a popup when you click the cluster and it will show a popup list of names of the markers inside the cluster. I found a code and it does exactly what I’m looking for: a list popup showing the names of the markers inside the cluster and when you click on the different names on the popup list the related marker popup appears. But when I added the code to my project, I get the error, “reference error onclick is not defined” and the function won't run. I research this error and I found it’s not good practice to use the onclick function in my javascript/HTML in the way the code is written. My question is, how do I rewrite the script to work the way the example I’m using. Here’s a link to the example I’m using to show what I’m trying to accomplish (and this example works when I download it and run it…no error) - http://www.digital-geography.com/working-with-clusters-in-leaflet-increasing-useability/. And I also included my script. Any help on how to make this work will greatly be appreciated. Or, I’m open to achieve this in a different way. Thanks in advance!



function openPopUp(id, clusterId){
map.closePopup(); //which will close all popups
map.eachLayer(function(layer){ //iterate over map layer
if (layer._leaflet_id == clusterId){ // if layer is markerCluster
layer.spiderfy(); //spiederfies our cluster
}
});
map.eachLayer(function(layer){//iterate over map rather than clusters
if (layer._leaflet_id == id){// if layer is marker
layer.openPopup();
}
});

markers.on('clusterclick', function(a){
if(a.layer._zoom == 6){
var myText = '<ul>';
for (feat in a.layer._markers){
myText += '<li><u onclick="openPopUp"(' + a.layer._markers[feat]._leaflet_id + ',' + a.layer._leaflet_id + ')>' + a.layer._markers[feat].feature.properties['cityName2'] + ',' + a.layer._markers[feat].feature.properties['cityName2'] + '</u></li>';
}
myText += '</u>';
var popup = L.popup().setLatLng([a.layer._cLatLng.lat,a.layer._cLatLng.lng]).setContent(myText).openOn(map);

}
})









share|improve this question














I created a map using Leaflet with clusters. I wanted to add a popup when you click the cluster and it will show a popup list of names of the markers inside the cluster. I found a code and it does exactly what I’m looking for: a list popup showing the names of the markers inside the cluster and when you click on the different names on the popup list the related marker popup appears. But when I added the code to my project, I get the error, “reference error onclick is not defined” and the function won't run. I research this error and I found it’s not good practice to use the onclick function in my javascript/HTML in the way the code is written. My question is, how do I rewrite the script to work the way the example I’m using. Here’s a link to the example I’m using to show what I’m trying to accomplish (and this example works when I download it and run it…no error) - http://www.digital-geography.com/working-with-clusters-in-leaflet-increasing-useability/. And I also included my script. Any help on how to make this work will greatly be appreciated. Or, I’m open to achieve this in a different way. Thanks in advance!



function openPopUp(id, clusterId){
map.closePopup(); //which will close all popups
map.eachLayer(function(layer){ //iterate over map layer
if (layer._leaflet_id == clusterId){ // if layer is markerCluster
layer.spiderfy(); //spiederfies our cluster
}
});
map.eachLayer(function(layer){//iterate over map rather than clusters
if (layer._leaflet_id == id){// if layer is marker
layer.openPopup();
}
});

markers.on('clusterclick', function(a){
if(a.layer._zoom == 6){
var myText = '<ul>';
for (feat in a.layer._markers){
myText += '<li><u onclick="openPopUp"(' + a.layer._markers[feat]._leaflet_id + ',' + a.layer._leaflet_id + ')>' + a.layer._markers[feat].feature.properties['cityName2'] + ',' + a.layer._markers[feat].feature.properties['cityName2'] + '</u></li>';
}
myText += '</u>';
var popup = L.popup().setLatLng([a.layer._cLatLng.lat,a.layer._cLatLng.lng]).setContent(myText).openOn(map);

}
})






javascript leaflet






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 1:25









TonyTTonyT

8211




8211













  • "reference error onclick is not defined" Is that really the exact error text? Sounds very strange

    – CertainPerformance
    Nov 13 '18 at 1:26













  • Sorry, it say "ReferenceError: openPopUp is not defined[Learn More]"

    – TonyT
    Nov 13 '18 at 1:28



















  • "reference error onclick is not defined" Is that really the exact error text? Sounds very strange

    – CertainPerformance
    Nov 13 '18 at 1:26













  • Sorry, it say "ReferenceError: openPopUp is not defined[Learn More]"

    – TonyT
    Nov 13 '18 at 1:28

















"reference error onclick is not defined" Is that really the exact error text? Sounds very strange

– CertainPerformance
Nov 13 '18 at 1:26







"reference error onclick is not defined" Is that really the exact error text? Sounds very strange

– CertainPerformance
Nov 13 '18 at 1:26















Sorry, it say "ReferenceError: openPopUp is not defined[Learn More]"

– TonyT
Nov 13 '18 at 1:28





Sorry, it say "ReferenceError: openPopUp is not defined[Learn More]"

– TonyT
Nov 13 '18 at 1:28












1 Answer
1






active

oldest

votes


















1














Because .setContent can accept an HTMLElement rather than just an HTML string, all you need to do is pass in an element with the listener attached, instead of the HTML string - construct the elements explicitly with createElement, and use addEventListener on the element you want to attach the listener to:



markers.on('clusterclick', function(a) {
if (a.layer._zoom !== 6) return;
const ul = document.createElement('ul');
a.layer._markers.forEach(({ _leaflet_id, feature }) => {
const { cityName2 } = feature.properties;
const li = ul.appendChild(document.createElement('li'));
const u = li.appendChild(document.createElement('u'));
u.addEventListener('click', () => openPopUp(_leaflet_id, a.layer._leaflet_id));
u.textContent = cityName2 + ',' + cityName2;
});
const popup = L
.popup()
.setLatLng([a.layer._cLatLng.lat, a.layer._cLatLng.lng])
.setContent(ul)
.openOn(map);
})





share|improve this answer


























  • Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

    – TonyT
    Nov 13 '18 at 2:46











  • Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

    – TonyT
    Nov 13 '18 at 2:48













  • Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

    – CertainPerformance
    Nov 13 '18 at 2:51











  • I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

    – TonyT
    Nov 17 '18 at 23:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272438%2fhow-to-rewrite-an-onclick-function-in-my-javascript-to-call-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Because .setContent can accept an HTMLElement rather than just an HTML string, all you need to do is pass in an element with the listener attached, instead of the HTML string - construct the elements explicitly with createElement, and use addEventListener on the element you want to attach the listener to:



markers.on('clusterclick', function(a) {
if (a.layer._zoom !== 6) return;
const ul = document.createElement('ul');
a.layer._markers.forEach(({ _leaflet_id, feature }) => {
const { cityName2 } = feature.properties;
const li = ul.appendChild(document.createElement('li'));
const u = li.appendChild(document.createElement('u'));
u.addEventListener('click', () => openPopUp(_leaflet_id, a.layer._leaflet_id));
u.textContent = cityName2 + ',' + cityName2;
});
const popup = L
.popup()
.setLatLng([a.layer._cLatLng.lat, a.layer._cLatLng.lng])
.setContent(ul)
.openOn(map);
})





share|improve this answer


























  • Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

    – TonyT
    Nov 13 '18 at 2:46











  • Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

    – TonyT
    Nov 13 '18 at 2:48













  • Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

    – CertainPerformance
    Nov 13 '18 at 2:51











  • I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

    – TonyT
    Nov 17 '18 at 23:35
















1














Because .setContent can accept an HTMLElement rather than just an HTML string, all you need to do is pass in an element with the listener attached, instead of the HTML string - construct the elements explicitly with createElement, and use addEventListener on the element you want to attach the listener to:



markers.on('clusterclick', function(a) {
if (a.layer._zoom !== 6) return;
const ul = document.createElement('ul');
a.layer._markers.forEach(({ _leaflet_id, feature }) => {
const { cityName2 } = feature.properties;
const li = ul.appendChild(document.createElement('li'));
const u = li.appendChild(document.createElement('u'));
u.addEventListener('click', () => openPopUp(_leaflet_id, a.layer._leaflet_id));
u.textContent = cityName2 + ',' + cityName2;
});
const popup = L
.popup()
.setLatLng([a.layer._cLatLng.lat, a.layer._cLatLng.lng])
.setContent(ul)
.openOn(map);
})





share|improve this answer


























  • Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

    – TonyT
    Nov 13 '18 at 2:46











  • Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

    – TonyT
    Nov 13 '18 at 2:48













  • Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

    – CertainPerformance
    Nov 13 '18 at 2:51











  • I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

    – TonyT
    Nov 17 '18 at 23:35














1












1








1







Because .setContent can accept an HTMLElement rather than just an HTML string, all you need to do is pass in an element with the listener attached, instead of the HTML string - construct the elements explicitly with createElement, and use addEventListener on the element you want to attach the listener to:



markers.on('clusterclick', function(a) {
if (a.layer._zoom !== 6) return;
const ul = document.createElement('ul');
a.layer._markers.forEach(({ _leaflet_id, feature }) => {
const { cityName2 } = feature.properties;
const li = ul.appendChild(document.createElement('li'));
const u = li.appendChild(document.createElement('u'));
u.addEventListener('click', () => openPopUp(_leaflet_id, a.layer._leaflet_id));
u.textContent = cityName2 + ',' + cityName2;
});
const popup = L
.popup()
.setLatLng([a.layer._cLatLng.lat, a.layer._cLatLng.lng])
.setContent(ul)
.openOn(map);
})





share|improve this answer















Because .setContent can accept an HTMLElement rather than just an HTML string, all you need to do is pass in an element with the listener attached, instead of the HTML string - construct the elements explicitly with createElement, and use addEventListener on the element you want to attach the listener to:



markers.on('clusterclick', function(a) {
if (a.layer._zoom !== 6) return;
const ul = document.createElement('ul');
a.layer._markers.forEach(({ _leaflet_id, feature }) => {
const { cityName2 } = feature.properties;
const li = ul.appendChild(document.createElement('li'));
const u = li.appendChild(document.createElement('u'));
u.addEventListener('click', () => openPopUp(_leaflet_id, a.layer._leaflet_id));
u.textContent = cityName2 + ',' + cityName2;
});
const popup = L
.popup()
.setLatLng([a.layer._cLatLng.lat, a.layer._cLatLng.lng])
.setContent(ul)
.openOn(map);
})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 2:51

























answered Nov 13 '18 at 1:32









CertainPerformanceCertainPerformance

79.3k143865




79.3k143865













  • Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

    – TonyT
    Nov 13 '18 at 2:46











  • Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

    – TonyT
    Nov 13 '18 at 2:48













  • Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

    – CertainPerformance
    Nov 13 '18 at 2:51











  • I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

    – TonyT
    Nov 17 '18 at 23:35



















  • Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

    – TonyT
    Nov 13 '18 at 2:46











  • Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

    – TonyT
    Nov 13 '18 at 2:48













  • Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

    – CertainPerformance
    Nov 13 '18 at 2:51











  • I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

    – TonyT
    Nov 17 '18 at 23:35

















Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

– TonyT
Nov 13 '18 at 2:46





Got a quick question, I thought I could fix it but I can't figure it out. When the list pop up it's showing undefined for the information that goes in that are (cityName2). Any ideas? Everything else work perfect.

– TonyT
Nov 13 '18 at 2:46













Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

– TonyT
Nov 13 '18 at 2:48







Sorry, not undefined. It say, [object Object],. It reading in the data from a csv file.

– TonyT
Nov 13 '18 at 2:48















Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

– CertainPerformance
Nov 13 '18 at 2:51





Oops, looks like you have to go through the properties sub-object as well. const { cityName2 } = feature.properties;, see edit

– CertainPerformance
Nov 13 '18 at 2:51













I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

– TonyT
Nov 17 '18 at 23:35





I wanted to let you know your fix worked! I'm sorry I missed adding that comment letting you know. Thanks again for your help!

– TonyT
Nov 17 '18 at 23:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272438%2fhow-to-rewrite-an-onclick-function-in-my-javascript-to-call-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values