When I hover over different counties on my map, a div updates whether or not the county voted republican or democratic in the 2020 election (among other things). It will either say it voted republican or democrat in a <h2> tag.

I have a column in the GeoJSON that has either red or blue hex codes depending on the majority vote of the county. How can I pull the value from that column in the GeoJSON to change the color of the text displayed in the div?

Here's a screenshot of the div

enter image description here

I want to make the <h2> text color (in this county's case "Democrat") correspond to the elecVaxData_voteColor value. How can I do this?

Let me know if more info is needed.

Here's the code

var map = L.map("map", {
  center: [40, -101],
  zoom: 5,

// add basemap

// add basemap labels
map.getPane("baselabels").style.zIndex = 600;
  .provider("CartoDB.PositronOnlyLabels", {
    pane: "baselabels",
    interactive: false,
    attribution: '| <a href="https://data.cdc.gov/Vaccinations/COVID-19-Vaccinations-in-the-United-States-County/8xkx-amqh/data">County data</a> | <a href="https://data.cdc.gov/Vaccinations/COVID-19-Vaccinations-in-the-United-States-Jurisdi/unsk-b7fc/data">State data</a> | Map: <a href="https://weircf.wixsite.com/e-portfolio">Chip Weir</a>',

var infoCounties = L.control();
infoCounties.onAdd = function (map) {
  this._div = L.DomUtil.create("div", "info"); // create a div with a class "info"
  return this._div;

// method that we will use to update the control based on feature properties passed
infoCounties.update = function (props) {
  this._div.innerHTML = props ?
    "<b>" +
    props.elecVaxData_county +
    ", " +
    props.elecVaxData_Recip_State +
    " (" +
    props.elecVaxData_PopClass +
    ")" +
    "</b><br /><b>" +
    props.elecVaxData_Series_Complete_Pop_Pct +
    "</b>" +
    " of the population is fully vaccinated " +
    "</b><br /><br />" +
    "This county voted majority " +
    "<h2 style = 'color: props.elecVaxData_voteColor;'>" +
    props.elecVaxData_full +
    "</h2>" +
    " in the 2020 Presidential election" :
    "Hover over a county";

What you're doing is pretty crude, but without too much focus on all the things that can go wrong and the many ways one could do this better, try changing;

"<h2 style = 'color: props.elecVaxData_voteColor;'>"


"<h2 style = 'color: "+props.elecVaxData_voteColor+";'>"

to make your property statement part of the JS code instead of the text content, given that props.elecVaxData_voteColor is a valid CSS color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.