I'm a little way through solving my task of creating a dropdown containing the insurance companies, then a second dropdown showing the insurance types that insurance company offers e.g. property, fleet or motortrade (insurance type offered incudes a number).) Once both select dropdowns have been selected display the relevant number.
At the moment this is as far as I've got thanks to some help below http://codepen.io/anon/pen/yoEiL
Heres the HTML:
<form action="" method="get" class="insurance-numbers">
<div>
<select id="company">
</select>
</div>
<div>
<select id="insurance-type">
</select>
</div>
<div id="insurance-number"></div>
</form>
And Javascript:
var insurancecompanies = [
{
name : 'Advent',
property : '01242 674 674',
fleet : '',
motortrade : ''
},
{ name : 'Allianz',
property : '0844 412 9988',
fleet : '0800 587 5858',
motortrade : ''
},
{ name : 'Aviva',
property : '0800 015 1498',
fleet : '0800 246 876',
motortrade : '0800 246 876'
},
{ name : 'AXA',
property : '0870 900 0867',
fleet : '0870 900 0860',
motortrade : '0870 900 1753'
},
{ name : 'Catlin',
property : '',
fleet : '0800 066 5364',
motortrade : ''
},
{ name : 'Chartis',
property : '',
fleet : '0844 477 6544',
motortrade : ''
},
{ name : 'Clegg Gifford',
property : '',
fleet : '',
motortrade : '01708 729 529'
},
{ name : 'Equity Red Star',
property : '',
fleet : '0845 609 1235',
motortrade : ''
},
{ name : 'Highway/LV',
property : '',
fleet : '0845 373 1244',
motortrade : ''
},
{ name : 'NIG',
property : '',
fleet : '0845 300 4644',
motortrade : '0845 300 4644'
},
{ name : 'QBE',
property : '',
fleet : '01245 272 700',
motortrade : ''
},
{ name : 'Royal Sun Alliance',
property : '0845 077 0120',
fleet : '0845 675 0404',
motortrade : '0845 077 0119'
},
{ name : 'Summit',
property : '',
fleet : '01254 396 655',
motortrade : ''
},
{ name : 'Zurich',
property : '0845 300 2055',
fleet : '0800 300 2055',
motortrade : ''
}
];
function findCompany(name) {
var i = 0, len = insurancecompanies.length;
for (i; i < len; i += 1) {
if (insurancecompanies[i].name === name) {
return insurancecompanies[i];
}
}
return null;
};
var dropdown = [], i = 0, len = insurancecompanies.length;
for (i; i < len; i += 1) {
dropdown.push(insurancecompanies[i].name);
}
$.each( dropdown, function( i, val ) {
$("#company").append( "<option val=\""+ val +"\">" + val + "</option>" );
});
$('#company').on('change', function() {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
var selectedInsurance = findCompany(valueSelected);
for (i in selectedInsurance) {
$("#insurance-type").append( "<option val=\""+ selectedInsurance[i] +"\">" + selectedInsurance[i] + "</option>" );
}
});
At the moment it displays the list of insurance companies correctly. It then selects the company chosen on change. I need it to then display the insurance types it offers (ignoring the ones it dosn't (ones with no numbers) then upon that selection display the number in the div#insurance-number.
So to demonstarte if i selected Allianz, the second dropdown would contain 'property' and 'fleet'. If the user selected 'fleet' the number '0800 587 5858' would get appended to the div#insurance-number'.
sorry if this is confusing I really need some help with this that is all.