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
ArVan
  • 4,225
  • 8
  • 36
  • 58

2 Answers2

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.

My Head Hurts
  • 37,315
  • 16
  • 75
  • 117
saintedlama
  • 6,838
  • 1
  • 28
  • 46
  • 1
    According to this other answer http://stackoverflow.com/questions/7851868/whats-the-proper-value-for-a-checked-attribute-of-an-html-checkbox it seems your usage of the checked attribute may be incorrect. – wprl Dec 21 '16 at 22:28
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

Vontei
  • 1,727
  • 2
  • 14
  • 16