4

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

2 Answers 2

9

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.

1
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.