· 6 years ago · Jun 20, 2019, 05:38 AM
1var EmailDomainSuggester = {
2
3 domains: ["yahoo.com", "gmail.com", "google.com", "hotmail.com", "me.com", "aol.com", "mac.com", "live.com", "comcast.com", "googlemail.com", "msn.com", "hotmail.co.uk", "yahoo.co.uk", "facebook.com", "verizon.net", "att.net", "gmz.com", "mail.com"],
4
5 bindTo: $('#email'),
6
7 init: function() {
8 this.addElements();
9 this.bindEvents();
10 },
11
12 addElements: function() {
13 // Create empty datalist
14 this.datalist = $("<datalist />", {
15 id: 'email-options'
16 }).insertAfter(this.bindTo);
17 // Corelate to input
18 this.bindTo.attr("list", "email-options");
19 },
20
21 bindEvents: function() {
22 this.bindTo.on("keyup", this.testValue);
23 },
24
25 testValue: function(event) {
26 var el = $(this),
27 value = el.val();
28
29 // email has @
30 // remove != -1 to open earlier
31 if (value.indexOf("@") != -1) {
32 value = value.split("@")[0];
33 EmailDomainSuggester.addDatalist(value);
34 } else {
35 // empty list
36 EmailDomainSuggester.datalist.empty();
37 }
38
39 },
40
41 addDatalist: function(value) {
42 var i, newOptionsString = "";
43 for (i = 0; i < this.domains.length; i++) {
44 newOptionsString +=
45 "<option value='" +
46 value +
47 "@" +
48 this.domains[i] +
49 "'>";
50 }
51
52 // add new ones
53 this.datalist.html(newOptionsString);
54 }
55
56}
57
58EmailDomainSuggester.init();