Run script inside hmtl of field formatter

Hi @Elaak,

thanks for your interesting question.

To make sure I understand correctly - you are using a field formatter to render a field as a button (or even a form) that should submit a request? That's a pretty cool idea to get custom UI into Kibana, although a bit hacky :slight_smile: .

I can see how the event listener workaround could work, you could add a custom data attribute like data-trigger-post-action="<the value of the field>" to your rendered html button. In your imported javascript you would have to install a click handler on the document that checks the target element on each click and whether it contains the data attribute and trigger the request if it's the case. The you don't need inline scripts.

Another possibility which is probably simpler is getting rid of javascript in the first place. You could use a regular html form with a nested submit button using <form target="_blank". Post requests shouldn't be a problem and it will open in a new tab so the current page is not lost.

If your use case is to add functionality to Discover in particular, you can also consider providing a separate "doc view" which will render as a tab (like the table and json view) for each row of discover: Inject front end code into Discover from custom plugin
This is the recommended way, as you have full control over the rendered html and attached event listeners. Plus this is an official API so we will support it and make sure it doesn't break because of an unrelated change which could always happen with the more hacky solutions.
The location in the code changed, its now https://github.com/elastic/kibana/tree/master/src/legacy/core_plugins/kbn_doc_views/public