Sam Short Sam Short - 11 days ago 4x
HTML Question

HTML: set id for onclick from list

Apologies if this question has already been asked. I'm new to HTML and I'm not familiar with the words I should use to find help with.

I'm using Flask and HTML to make a website.

My code:

<!DOCTYPE html>
<h3>Click the result you want to investigate</h3>
{% for r in results %}
<p id=r["links"] onclick="myFunction(id)"> {{r["title"]}} ({{r["address_snippet"]}}) </p>
{% endfor %}

function myFunction(id) {
document.getElementById(id).innerHTML = "CLICKED HERE";

I'm trying to print out a list of names, and etc to the screen. The user will click one name, this name will then be returned to a Python function for further analysis.

As a first attempt, I just want to change the text from the name to "CLICKED HERE". However, regardless of which name I click, only the first entry changes.

I can't figure out how to set the id from the container. Any help would be appreciated!


You need to update your code like this, so you get the actual value from the variable r["links"], by adding the brackets {{r["links"]}}

<p id="{{r["links"]}}" onclick="myFunction('{{r["links"]}}')"