I keep getting an error when Im trying to find the value of a select2 selected element via:-
alert($("#txtCompetencesSuppliersAdd-Supplier").select2('data')[0]['supplierID']);
So, I thought I could test it with typeof undefined as:-
if (typeof $("#txtCompetencesSuppliersAdd-Supplier").select2('data')[0]['supplierID'] === undefined) {
alert("Please select a supplier first");
} else {
alert($("#txtCompetencesSuppliersAdd-Supplier").select2('data')[0]['supplierID']);
}
But still get the error message:-
Uncaught TypeError: Cannot read properties of undefined (reading 'supplierID')
The alert works fine if an option is selected, I just want to show a warning if one isnt selected.
Any suggestions?
Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.