Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade radio button checked condition

I have form in Jade file, and I want it to be pre-filled with some values that come from backend.In the form, there are radio buttons for user gender.

input(type="radio", name="gender_filter", value="1") Male
input(type="radio", name="gender_filter", value="0") Female

Now I also have this variable gender_param, and I want the corresponding button to be selected when page loads. In PHP, I could do this:

<input type="radio" name="gender_filter", value="1" <?php echo ($gender_param==1)?"checked":''; ?>>

Is there the corresponding syntax for Jade? Or I need to write it in the long way with line duplicates like

- if gender_param==1
   input(type="radio", name="gender_filter", value="1", checked) Male
- else
   input(type="radio", name="gender_filter", value="1") Male
like image 535
ArVan Avatar asked Dec 16 '22 16:12

ArVan


2 Answers

You can do this in jade even simpler than in PHP:

input(type="radio", name="gender_filter", value="1", checked=gender=="male")
| Male
input(type="radio", name="gender_filter", value="0", checked=gender=="female")
| Female

This code block expects gender to be a variable passed from the backend to the view.

like image 79
saintedlama Avatar answered Feb 27 '23 19:02

saintedlama


For Jade: here are a few input types and the shorthand for keeping the checked value once submitting the form, i.e. so you can populate an edit page...

Radio Button:

input(type="radio" name="stars" value='1' id='stars' checked=theAlbum.rating=='1')

Select box:

option(selected = theAlbum.genre) #{theAlbum.genre}

option Jazz option Rock option Rap option Dance

Checkbox:

checked=theAlbum.checkbox

like image 35
Vontei Avatar answered Feb 27 '23 18:02

Vontei