2

I have created Radiobutton group and first one is coming as auto selected. how can i remove that?

var radio = CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.RADIO_BUTTON)
radio.setOnChangeAction(CardService.newAction()
     .setFunctionName("setType")
      .setLoadIndicator(CardService.LoadIndicator.SPINNER))
radio.setFieldName("event_type").setTitle("Select Type")
radio.addItem('item1',1,false);
radio.addItem('item2',2,false);
radio.addItem('item3',3,false);
vinay narayana
  • 197
  • 1
  • 2
  • 10
  • 2
    You could add a 4th option before `item1` that went`radio.addItem('choose an option',1,false);` and have that as the "default" – AMolina Jul 02 '19 at 07:40
  • Its a good one. but do we have any alternative for this. Instead of adding new one can we make deselect everything? – vinay narayana Jul 03 '19 at 06:18
  • 1
    After some research I found [this documentation](https://www.sitepoint.com/html-checkbox-radio-button-defaults/) on radio buttons, and part of it says "When a checkbox is unchecked or no radio button is selected, nothing is sent for those fields in the HTTP POST. Unlike other input boxes, there’s no default value — the POSTed field simply doesn’t exist. To make it work correctly, you need to check its existence on the server." So there has to be a default selected. – AMolina Jul 03 '19 at 06:36
  • the fact that an item is visually selected but nothing is returned in `e.formInput` is really a bad bad behaviour – ValLeNain Apr 18 '22 at 15:02

1 Answers1

0

Since you have set values for all the items to false, it is showing the first item as auto selected.

radio.addItem('item1',1,false);
radio.addItem('item2',2,false);
radio.addItem('item3',3,false);

If you want to show any of the items as default, you can set one of the values to true,

radio.addItem('item2',2,true);

However, in both the cases, when you submit the form, it returns the value of the item that you have selected, not the autoselected one.

You can test it like this,

var radio = CardService.newSelectionInput()
                       .setType(CardService.SelectionInputType.RADIO_BUTTON)
                       .setFieldName("event_type").setTitle("Select Type")
                       .addItem('item1',1,false)
                       .addItem('item2',2,false)
                       .addItem('item3',3,false);

var button = CardService.newTextButton().setText("Submit")
           .setOnClickAction(CardService.newAction().setFunctionName('test'));



function test(e){
  Logger.log(e.formInputs.event_type);
}

The logs shows value of selected radio-button as [1.0] if item1 is selected.