I would like to use a javascript variable (an email address) that is obtained by clicking on a map point (mapbox). I would like to insert this variable in a html/php form as the mailto address. But I'm have spent hours trying to get the variable out of the onclick function, with no avail. This is my last try:
var agencyemail;
map.on('click', 'unclustered-point', function (e) {
agencyemail = e.features[0].properties.email;
// the variable functions correctly within the onclick function... and then more
}
This is not working apparently. From here I would like to insert the captured variable into the php form
$et_email_to =
'<script>
document.write(agencyemail);
</script>' ;
I have also tried to use sessionStorage, localStorage or let but I still haven't figured it out. Thanks for your help.
EDIT: I used the solutions proposed here. I simply inserted the variable in a form hidden field value.
map.on('click', 'unclustered-point', function (e) {
agencyemail = e.features[0].properties.email;
document.getElementById("agencyemail").value = agencyemail;
... and in html
<input type="hidden" value="" id="agencyemail" name="agencyemail"/>
Thanks for the help.