-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetTrainClasses.js
67 lines (48 loc) · 1.92 KB
/
getTrainClasses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
let trainNo = document.querySelector("#trainNumber1");
let tbody = document.querySelector('#tableAppend');
const trainClass = new Map([
["1A", "First AC"],
["EA", "Executive Anubhati"],
["EC", "AC Executive Class"],
["2A", "Second AC"],
["FC", "First Class"],
["3A", "Third AC"],
["3E", "Third AC Economy"],
["CC", "AC Chair Car"],
["SL", "Sleeper"],
]);
let button = document.querySelector('.submit-btn1');
let flag1 = true;
let resultBox1 = document.querySelector('.resultBox1');
button.addEventListener('click',()=>{
event.preventDefault(); // Stops webpage from reloading automatically after form submission
console.log(trainNo.value);
if(trainNo.value=="number"){
alert("Please select train number");
return;
}
// API Call
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': 'a1e0032c9amsh0c59e81fe5967c8p1e443bjsn602da0c29fd4', // Change rapidapi key here
'X-RapidAPI-Host': 'irctc1.p.rapidapi.com'
}
};
fetch(`https://irctc1.p.rapidapi.com/api/v1/getTrainClasses?trainNo=${trainNo.value}`, options)
.then(response => response.json())
.then(response => {
console.log(response);
for(let i=0; i<response.data.length; i++){
let tr = document.createElement('tr');
tr.innerHTML = `<td><a href="#">${response.data[i]}</a></td><td class="amount">${trainClass.get(response.data[i])}</td>`;
tbody.appendChild(tr);
}
})
.catch(err => console.error(err));
// Show resultBox
if(flag1){
resultBox1.style.display = 'grid';
flag1 = false;
}
});