Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Uncaught TypeError: Cannot read properties of undefined

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']);

Open in new window

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']);
}

Open in new window

But still get the error message:-

Uncaught TypeError: Cannot read properties of undefined (reading 'supplierID')

Open in new window

The alert works fine if an option is selected, I just want to show a warning if one isnt selected.


Any suggestions?

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial