Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript function calls within Django form fields

I'd like to create a form where I have checkboxes, and when clicked, they open separate textareas for the user to enter more information in.

If I want to use Django's dynamically created form fields, is there a way that I can put a function call in for each checkbox.

like image 862
Vernon Avatar asked Aug 21 '26 02:08

Vernon


1 Answers

You can dynamically add event handlers using JavaScript. You can add a script that, once the page is loaded, will find all checkboxes you want and add the handlers there. In jQuery, you can write something like this:

$(document).ready(function() {
    $(".my_form input[type=checkbox]").change(function() {
          //Some code here
    });
});

Be careful, I have not tested the code above! But should be enough to get you started.

like image 88
Denilson Sá Maia Avatar answered Aug 23 '26 14:08

Denilson Sá Maia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!