JSFiddle - React, Tailwind, and code Playground
by secretgspot
HTML
<h1>Dry pattern |p0-a</h1>
<p>Dry example</p>
JavaScript
$('.someCheckbox').click(function(){
var checked = this.checked,
fields = ['carModel', 'carYear', 'carMiles', 'carTint'];
/*
What are we repeating?
1. input_ precedes each field name
2. accessing the same array for settings
3. repeating value resets
What can we do?
1. programmatically generate the field names
2. access array by key
3. merge this call using terse coding (ie. if checked,
set a value, otherwise don't)
*/
$.each(fields, function(i,key){
$('#input_' + key).val(checked ? defaultSettings[key] : '');
});
});