How to expose a form when a radio button is checked?

2024/10/9 2:28:18

Consider the following sample html template,

<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y">y</input><br><form name="form1"> Form1... </form><form name="form2"> Form2... </form></body>
</html>

The flask code for rendering the template,

from flask import (Flask, render_template)app = Flask(__name__)@app.route('/')
def main():return render_template('sample.html')

Once the template is rendered by flask, I would like to show form1 if x radio button is checked and form2 if y radio button is check. Is it possible to do so using jinja templates syntax ?

Something similar to this

<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y">y</input><br>{% if x %}<form name="form1"> Form1... </form>{% elif y %}<form name="form2"> Form2... </form>{% else %}Something...{% endif %}</body>
</html>

Also is there something similar to document.getElementById() in jinja templates ?

Answer

Templating languages like Jinja work with backend input only. They serve to render the initial HTML. To do active control of the page after it has been loaded, you will want to use javascript (and a tiny bit of css). First you will need a css class that tells a form to stay out of view until you decide to show it with the display: none property. Lets call this class "hidden". Your css would look something like

.hidden {display: none;
}

Secondly, for both forms to start hidden, you will need to add that class to their HTML declaration, like:

<form class="hidden" name="form1"> Form1... </form>
<form class="hidden" name="form2"> Form2... </form>

You will then want to add Javascript listeners to each of the radio buttons. The onchange event is ideal for this situation. You will want to remove or add that "hidden" class to the correct form when either of the radio buttons changes state. Here I use the "checked" property of the buttons to see if they were just changed to the ON state or the OFF state:

radioX.addEventListener('change',(event)=>{if (event.target.checked){form1.classList.remove("hidden");form2.classList.add("hidden");} else {form1.classList.add("hidden");form2.classList.remove("hidden");}
});

Finally your whole HTML file would look something like :

<html>
<head><style>.hidden{display:none;}</style>
</head>
<body><input id="radio-x" type="radio" name="form-toggle">x</input><br><input id="radio-y" type="radio" name="form-toggle">y</input><br><form class="hidden" name="form1"> Form1... </form><form class="hidden" name="form2"> Form2... </form><script>const form1 = document.querySelector("form[name='form1']");const form2 = document.querySelector("form[name='form2']");document.querySelector('#radio-x').addEventListener('change',(event)=>{if (event.target.checked){form1.classList.remove("hidden");form2.classList.add("hidden");} else {form1.classList.add("hidden");form2.classList.remove("hidden");}});document.querySelector('#radio-y').addEventListener('change',(event)=>{if (event.target.checked){form1.classList.add("hidden");form2.classList.remove("hidden");} else {form1.classList.remove("hidden");form2.classList.add("hidden");}});</script>
</body>
</html>
https://en.xdnf.cn/q/118638.html

Related Q&A

How to generate perlin noise in pygame?

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:But I want something like islands or rivers. Heres my code: #SetUp# import pygame, sys, random pygam…

Inputting range of ports with nmap optparser

This is the scriptimport nmap import optparsedef nmapScan(tgtHost,tgtPort):nmScan = nmap.PortScanner()nmScan.scan(tgtHost,tgtPort)state=nmScan[tgtHost][tcp][int(tgtPort)][state]print "[*] " +…

Implementing a Python algorithm for solving the n-queens problem efficiently

I am working on a project that requires me to solve the n-queens problem efficiently using Python. I have already implemented a basic recursive algorithm to generate all possible solutions, but I am lo…

Annotations with pointplot

I am using a pointplot in seaborn.import seaborn as sns sns.set_style("darkgrid") tips = sns.load_dataset("tips") ax = sns.pointplot(x="time", y="total_bill", hu…

How do I get data in tuples and tuples in lists?

I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being(1,2)(in a 2 dimensional grid).manhattan=[[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11…

How to return a value from a table or dataframe given 2 inputs? Python

Lets say this is my dataframe:and the user inputs B and 2Then the function would return clemintineIs there a way to do this without using a bunch of if elif statements. The actual dataframe Im working …

tkinter progressbar for multiprocessing

I have a program that encrypts files and I used multiprocessing to make it faster, but I am having trouble with the tkinter progress bar. I have implemented it but it completes immediately or lags in b…

How to add and subtract in python

So I am making a statcalc and everything is working except adding. When I select the option to add it just skips it and says select an option. I was wondering whats wrong with it?numberstoadd = input(…

Python: deferToThread XMLRPC Server - Twisted - Cherrypy?

This question is related to others I have asked on here, mainly regarding sorting huge sets of data in memory.Basically this is what I want / have:Twisted XMLRPC server running. This server keeps seve…

How do I make a linear gradient with Python Turtle?

Im currently trying to replicate this image: https://i.sstatic.net/fymWE.jpg Im trying to make that gradient in the background but I have zero clue how to do it and theres basically nothing on the inte…