Select to view content in your preferred language

Want to make an HTML button within a Popup widget darken when it is hovered over, and have it emulate the appearance of being physically pressed down.

428
4
Jump to solution
07-26-2024 10:35 AM
andrewc213
Emerging Contributor

I have a FeatureLayer in my web app that contains tiles representing land units. When clicking on one, this Popup shows up:
popup.png
The "Get Parcel Detail" button is a recreation of this original button from a previous version of my web app:
button.png
The original button darkens like this when the cursor hovers over it:
button-hover.png
The original button has a sort of 3D appearance of being physically pressed down when the mouse button is held down on it:
button-active.png

I'm trying to recreate this behavior for my new button in my Popup; however, nothing has worked so far. I added the Popup's text and button like so:

export function formatContent(event: { graphic: { attributes: any; }; }) {
  const attributes = event.graphic.attributes;
  let text = "";
  // Only display the attributes if they exist
  text += attributes.UseType ? `<b>Use: </b>${attributes.UseType}` : "";
  text += attributes.UseDescription ? `; ${attributes.UseDescription}<br>` : "<br>";
  text += attributes.SitusAddress ? `<br><b>Address:</b><br>${attributes.SitusAddress}<br>` : `<br><b>Located in: </b>${attributes.TaxRateCity}`;
  text += attributes.SitusCity ? `${attributes.SitusCity}` : "";
  text += attributes.SitusZIP ? ` ${attributes.SitusZIP.substring(0, 5)}<br>` : "<br>";
  text += `<br><a
    role="button"
    button id="navigate-button"
    class="btn btn-default"
    style="
      display: inline-block;
      padding: 5px 10px;
      text-align: center;
      text-decoration: none;
      color: #000;
      background-color: #f8f9fa;
      border: 1px solid #343a40;
      border-radius: 4px;
      transition: background-color 0.3s ease;
    "
    onmouseover="this.style.backgroundColor='#e2e6ea';"
    onmouseout="this.style.backgroundColor='#f8f9fa';"
    onmousedown="this.style.backgroundColor='#ced4da';"
    onmouseup="this.style.backgroundColor='#e2e6ea';"
    >Get Parcel Detail</a>`;

 
    const textContent = new TextContent({
      text: textElement.innerHTML
    });
 
    return [textContent];
  }

I tried using the onmouse* events to at least emulate the darkening effect, but it seems like my program is not responding to those events, or any of the CSS styling code that I've tried to use, whether I add the CSS code to a separate .css file which I import, or inject directly into the HTML code. I've tried the following code between "Get Parcel Detail</a>" and "const textContent":

    const textElement = document.createElement('div');
    textElement.innerHTML = text;
 
    const navigateButton = textElement.querySelector('#navigate-button');
 
    if (navigateButton) {
      navigateButton.addEventListener('mouseover', function(this: HTMLAnchorElement) {
        this.style.backgroundColor = '#e2e6ea';
      });
 
      navigateButton.addEventListener('mouseout', function(this: HTMLAnchorElement) {
        this.style.backgroundColor = '#f8f9fa';
      });
 
      navigateButton.addEventListener('mousedown', function(this: HTMLAnchorElement) {
        this.style.backgroundColor = '#ced4da';
      });
 
      navigateButton.addEventListener('mouseup', function(this: HTMLAnchorElement) {
        this.style.backgroundColor = '#e2e6ea';
      });
    }
 
but that does not work either.
0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

The root cause of why event handlers within your HTML tags are ignored is discussed in this thread, along with some suggested workarounds.

View solution in original post

0 Kudos
4 Replies
Paco
by
Frequent Contributor

Hm.  i have a very simple but similar popup with an html button that changes color on hover.

this is my js/html for a button than says "Geology"-

`<a href="{NGMDB_URL}"
       target="blank" type="button" id="btn_geology" class="btn_geology" style="color:white;width:112px;padding:12px">Geology</a><br>`
 
and here is some of my css for that button to change color(using 'hover')-
 
  .btn_geology:hover {
    background-color: gray;
  }
0 Kudos
andrewc213
Emerging Contributor

I used CSS code very similar to yours for my button! Thanks!

0 Kudos
JoelBennett
MVP Regular Contributor

The root cause of why event handlers within your HTML tags are ignored is discussed in this thread, along with some suggested workarounds.

0 Kudos
andrewc213
Emerging Contributor

Thanks! This post in the thread that you linked helped a lot.

0 Kudos