Jump to:

1976 Posts in 1306 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Set Radio Button to be Check by default

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2105 Views
  • merrick_sd
    Avatar
    Community Member
    89 Posts

    Set Radio Button to be Check by default Link to this post

    Is it possible to set a radio button to be checked intially, or should i use jquery
    I'm sure I can use jquery to detect if its changed or clicked, but i'm a bit stuck when neither is checked

    Would be nice to know anyway, if altering php code below could make it checked to start with, if possible

    ---PHP code file --
    $BRvalues = array('Buy'=>'For Sale','Rent'=>'For Rent');
    $fields->push(new OptionsetField('buyrent','', $BRvalues,$propertybuyrent));
    ---PHP code file -

    which outputs

    <ul id="Form_HouseSearchForm_buyrent" class="optionset nolabel">

    <li class="odd valBuy">
    <input id="Form_HouseSearchForm_buyrent_Buy" name="buyrent" type="radio" value="Buy" class="radio" />
    <label for="Form_HouseSearchForm_buyrent_Buy">For Sale</label>
    </li>

    <li class="even valRent">
    <input id="Form_HouseSearchForm_buyrent_Rent" name="buyrent" type="radio" value="Rent" class="radio" />
    <label for="Form_HouseSearchForm_buyrent_Rent">For Rent</label>
    </li>

    </ul>

  • merrick_sd
    Avatar
    Community Member
    89 Posts

    Re: Set Radio Button to be Check by default Link to this post

    i have used jQuery in the end

    if ($("input[@name='buyrent']:checked").val() == 'Buy')
    {
    // Code for handling value 'a'
    //alert('Buy');
    }
    else if ($("input[@name='buyrent']:checked").val() == 'Rent')
    {
    // Code for handling value 'b'
          //alert('Rent');
          }
    else
    {
    // Code for handling 'c'
    //alert('else');
    //set first radio to be check by default
    $('div#housesearchform input:radio:nth(0)').attr("checked","checked");
    }

    2105 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.