Alpan Karaca
Alpan Karaca

Reputation: 968

How to get if checkbox is checked on flask

I'm using Bootstrap with Flask Python.

 request.form.get("name")
 #name is the name of the form element(checkbox)

 <label class="btn btn-danger pzt active">
     <input type="checkbox" name="name" value="1" data-id="0"> Check
 </label>

When checkbox is checked, parent label has class "active", I want to get if checked box is checked. Is there any way or methods?

Upvotes: 12

Views: 42737

Answers (2)

anurag619
anurag619

Reputation: 712

you can try the following:

HTML:

<div class="checkbox">
<label>
    <input type="checkbox" name="check" value="edit"> New entry
</label>
</div>

In flask:

 value = request.form.getlist('check') 

This will give you the value of the the checkbox. Here value will be a list.

value = [u'edit']

You can also get value of multiple checkboxes with same name attribute.

Upvotes: 20

TwilightSun
TwilightSun

Reputation: 2335

I'm not familiar with Flask, but I do know how HTTP works.

If you want to know if its checked on the server side, just check if that form field exists, if request.form.get("name") gives you NULL or exception, then the checkbox should be unchecked.

If you want to know it on the client side with javascript, you can use jQuery (as jQuery is a base component of Bootstrap) as $('xxxx').is(':checked') (replace xxxx with a valid selector).

Upvotes: 5

Related Questions