New to Django - Seeking assistance with concealing sections of a dynamically generated table

I am working on a dynamic table where I need to show or hide certain parts based on the state of corresponding checkboxes. When a checkbox is unchecked, I want to hide the specific section of the table with the class "newtable" for that row only. Each row in the table should operate independently in terms of visibility. If a checkbox is checked, additional information appears and must be filled out, while if it is not checked, no further action is required. I know how to use JavaScript to hide all instances of "newtable", but my challenge lies in hiding only those that are related to checked checkboxes. Any guidance on this matter would be highly appreciated.

Due to having variables from different models, I had to create this form manually since using Django's form functionalities was not feasible for achieving this setup. Your help is much appreciated!

<table>
<tr>
{% for x,y,z in stuff%}
    <td>{{ x.foo }}</td>
    <td>{% for item in y %}
        <input type="checkbox" name="stuff.{{ item.id }}" class="item" value="True" checked/> {{ item }}<br />        
    {%  endfor %}
    </td>
    </tr>
    <tr><td align=center colspan="5">
    <table class="newtable"  border=1>
    <tr><td>
    <input type="radio" name="pref_id{{ z.id }}"  value="1" checked> blah<br>

{%  endfor %}
</td>
</tr>
</table>
</tr>
    {% endfor %}

</table>

Answer №1

If you're looking to hide all the checkboxes that are checked in a table with the class .newtable, you can achieve this easily using jQuery.

<!DOCTYPE html>
<html>
<head>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
   </script>
    <script type="text/javascript">
        $(document).ready(function() {
            $(".newtable input[type=checkbox]:checked").hide();
        });
    </script>
</head>
<body>
    <table class="newtable">
        <tr><td><input type="checkbox" checked/></tr>
        <tr><td><input type="checkbox"></tr>
    </table>
</body>

If you prefer to hide the entire row instead of just the checkbox, you can do so with the following code:

$(".newtable input[type=checkbox]:checked").parent().parent().hide()

Another approach is to hide them during form generation, especially if you need to set some checkboxes as checked based on certain conditions:

{% if item.checked %}
    <input type="checkbox" checked style="display:none" ..../>      
{% else %}
    <input type="checkbox"  ..../>      
{% endif %}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to link a Javascript or Jquery function to an HTML form

I am looking to develop a form that can extract email addresses from input text. While I know there are many scripts available that can do this quite easily, my goal is to create one on my own. After reading, researching, and experimenting with differ ...

JavaScript Bingo Game - Create Interactive Cell Selection

Below is the HTML code that I am using to create a Bingo card: ... <th class="orange">B</th> <th class="orange">I</th> <th class="orange">N</th> ...

How can an Express.js server detect when a browser has been closed or reloaded?

Currently tackling a project with an Express.js server. One query I have is how can this server detect when one of its users (browser) has been closed or reloaded? Any insights on this would be greatly appreciated! ...

Can you explain the reference point used in the `drawImage()` function on a canvas?

Here is an inquiry concerning the usage of the drawImage() function. var x = (i-j)*(img[0].height/2) + (ctx.canvas.width/2)-(img[0].width/2); var y = (i+j)*(img[0].height/4); abposx = x + offset_x; abposy = y + offset_y; ctx.drawImage ...

Netlify encountered an error with mixed content, indicating that the page was successfully loaded over HTTPS, but it attempted to request an insecure HTTP

hey everyone, Recently, I deployed my vue-cli website project on Netlify. However, upon opening the website, I encountered the following error message: Mixed Content: The page at 'https://xxxx.netlify.app/' was loaded over HTTPS, but requested a ...

Discover the process of retrieving an element by its ID when dealing with dynamically generated IDs in AngularJS

I am creating a list using the following code snippet: <div ng-repeat="list in fileUploadList"> <div id="{{list}}Upload"></div> </div> Within the controller, I need to retrieve the element by ID, so I am utilizing this line of ...

Why is my index.tsx file not properly exporting? (React + Typescript)

I've developed a basic Context Provider that I'd like to package and distribute via npm. To package my code, I utilized the create-react-library tool. In my project, I've set up an index.tsx file that should serve as the entry point for im ...

"Why does the form.submit() function fail in IE9 when the form is in an iframe and the user is coming from Gmail

I have recently developed a function within my CodeIgniter framework that allows me to send emails with a backlink to my site. The link directs users to a page on my website that includes an iframe. Within this iframe, I have implemented a file input form ...

Displaying numerous database rows through SQL with the aid of PHP and JavaScript (specifically AJAX) on the frontend of a website

I am a beginner in the world of PHP and JavaScript (Ajax) and despite my efforts to find a solution by looking at various posts and websites, I have not been successful. My goal is to display all related database rows on the front end of a website. While ...

What is the best way to dynamically update a specific value within an object based on the current state in React/Next?

I have implemented a Context API where an object is set, and when the user clicks, the state changes. I need to update a specific value with the new state value. const initialState = { notification: false, setting: false, profile: false, } exp ...

What is the best way to sort the values of an associative array in JavaScript?

Consider the following associative array: array["sub2"] = 1; array["sub0"] = -1; array["sub1"] = 0; array["sub3"] = 1; array["sub4"] = 0; How can you efficiently sort this array in descending order based o ...

Developing play and pause capabilities using JavaScript for the existing audio player

Currently, I am developing a Chrome extension that includes an HTML popup with buttons to play audio files. However, I feel that my current approach is not the most efficient and I am struggling to find ways to simplify the process. The method I am using n ...

Approach to Using Bootstrap 4 with Intl-Tel-Input

I've been attempting to integrate intl-tel-input with bootstrap 4, but I'm facing an issue where the placeholder for the input is not showing up. I've tried various solutions found online, but none of them seem to work. Here's my HTML ...

how to ensure a consistent property value across all scopes in AngularJS

Here is my perspective <div ng-if="isMultiChoiceQuestion()"> <li class="displayAnswer" ng-repeat="choice in getMultiChoice() track by $index" ng-if="isNotEmpty(choice.text.length)"> <input type= ...

What is the process for retrieving the address of the connected wallet using web3modal?

I've been working on an application using next.js and web3. In order to link the user's wallet to the front-end, I opted for web3modal with the following code: const Home: NextPage = () => { const [signer, setSigner] = useState<JsonRpcSig ...

Searching by multiple parameters in Angular.js

I have a script that scans through a field and highlights the words related to the search input. I want this script to not only filter by title, but also by name. How can I adjust the code to filter both the title and name when searching? <li ng-repeat ...

The scope chain in Chrome v71 connects the anchor tag to the inner img tag

Ever since updating to Chrome v71, I've noticed a strange issue with the scope of an anchor tag that contains an img tag inside. Take a look at this snippet: <a href="#none" onclick="debugger;complete();"> <img src="https://clickmeuk.net/w ...

Unlocking the Secrets: Javascript Tricks for Retrieving Object Values

I have an item that contains data which I need to display. {"text": "active user active user213123 idle user234234234 loggedout userafdadf" }, To extract the content, I used the following code: Response = message.s ...

Angular does not alter the focus when a new component is loaded

Currently, I am working on resolving an accessibility issue with a screen reader in an Angular2 web application. When componentA (code shown below as Before) is loaded in Chrome, the entire browser window gains focus and the screen reader narrator announce ...

"Multiple instances of JavaScript files seem to be present when using Ajax navigation

Having some difficulties with AJAX navigation. The issue is that the JavaScript files loaded remain in the browser even after the new content is loaded, even when they are no longer in the DOM. These files appear as VM files in the browser console and cont ...