javascript - How to make first radio button selected by default in a list built with a for in loop using twig? -
i'm using twig make list of radio buttons database values. it's like:
{% usermembershiptype in usermembershiptypes %} <div class="ms_type noline"> <p>{{ usermembershiptype.description}}</p> <label class="ms_price ms_odd">${{ usermembershiptype.price }}<span>/year</span><input name="membership-type" type="radio" value="{{ usermembershiptype.id }}"/></label> <div class="clearfix"></div> </div> {% endfor %}
i want first radio button selected default when using in loop, i'm not sure how refer first radio button.
any advices? javascript , jquery solutions welcome.
you can use loop.index , if condition like
{% usermembershiptype in usermembershiptypes %} <div class="ms_type noline"> <p>{{ usermembershiptype.description}}</p> <label class="ms_price ms_odd">${{ usermembershiptype.price }}<span>/year</span> {% if loop.index == 1 %} <input name="membership-type" type="radio" value="{{ usermembershiptype.id }}" checked/> {% else %} <input name="membership-type" type="radio" value="{{ usermembershiptype.id }}"/> {% endif %} </label> <div class="clearfix"></div> </div> {% endfor %}
note: not tested
using loop.first
{% usermembershiptype in usermembershiptypes %} <div class="ms_type noline"> <p>{{ usermembershiptype.description}}</p> <label class="ms_price ms_odd">${{ usermembershiptype.price }}<span>/year</span> {% if loop.first %} <input name="membership-type" type="radio" value="{{ usermembershiptype.id }}" checked/> {% else %} <input name="membership-type" type="radio" value="{{ usermembershiptype.id }}"/> {% endif %} </label> <div class="clearfix"></div> </div> {% endfor %}
Comments
Post a Comment