Kostas Kon Kostas Kon - 3 months ago 20
HTML Question

Default checked button

Below code is html produced from opencart 2

No radio button is selected when loading page.

How can i have selected the Value="2" as default checked. (javascript or CSS)

<div id="payment-custom-field1" class="form-group custom-field" data-sort="0">
<label class="control-label">Invoice</label>
<div id="input-payment-custom-field1">
<div class="radio">
<label>
<input type="radio" value="2" name="custom_field[1]">
Receipt
</label>
</div>
<div class="radio">
<label>
<input type="radio" value="1" name="custom_field[1]">
Invoice
</label>
</div>
</div>
</div>


Solution with javascript

(function() {
document.getElementsByName("custom_field[1]")[0].checked=true;
})();

Answer
<input type="radio" id="sample" value="2" name="custom_field[1]">
Receipt
</label>

Using javascript:

document.getElementById("sample").checked = true;

Using html: You can simply add checked attribute to your html element

Using the element name:

(function() {
   document.getElementsByName("custom_field[1]")[0].checked=true;
})();

https://jsfiddle.net/pandiyancool/vb73q59w/