How can I replace a taxonomy / category text string, onclick using javascript in a .md file?

156 Views Asked by At

Remove / Change taxonomy on button click, assuming the GRAV user is logged in.

I have tried:

---
taxonomy:
    tag:
        - mountain
<code>
<script>
    function removeTg() {
    var ret = "mountain".replace('mountain','');
}
</script>
</code>
---
<button onclick="removeTg()">Remove tag</button>

Any idea how to make scripts execute and change .md file content / strings, in a section wrapped by?

---
 as non visible .md taxonomy page content.
---
1

There are 1 best solutions below

2
On

If you want to run a script by clicking on a button defined inside the Markdown content of a Grav page file (.md), the following snippet will work:

---
taxonomy:
    tag: mountain
---
<button onclick="removeTg()">Remove tag</button>
<script>
    function removeTg() {
        alert('Script is running')
    }
</script>

Of course, this doesn't do much. If you want to change to content of the .md file on the server, the script will need to submit a request to the server. A custom Grav plugin will then catch the request and update/save the .md file.

A few notes:

  • Why would you want to do this? This is what the Admin plugin is made for...
  • Markdown is meant for defining content and should be separated from logic.
  • Scripts (and styles) should be in their own files.
  • Twig templates then combine the content, layout, style and logic.