I am writing a JavaScript code in the VS code editor. But the application detects my code as TypeScript. What is the reason of this?
And my code below doesn't work because it sees my code as TypeScript. I cannot get the value of input element
var inputValue = document.getElementById("postId").value;
Here is my full code
var inputValue = document.getElementById("postId").value;
document.querySelector("#getOne").addEventListener('click', function(){getOne(inputValue)});
document.querySelector("#getAll").addEventListener('click', getAll)
function getOne(ids) {
alert(inputValue)
var url = "https://jsonplaceholder.typicode.com/posts";
var xhr = new XMLHttpRequest;
xhr.open('GET', url, true);
xhr.onload = function () {
if (this.status === 200) {
var posts = JSON.parse(this.responseText);
var html = '';
ids-=1;
posts.forEach(post => {
if(post.id === ids){
html = `
<div class="card">
<div class="card-header">
${posts[ids].title}
</div>
<div class="card-header">
your user ID is: ${posts[ids].userId}
</div>
<div class="card-header">
post ID is: ${posts[ids].id}
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>${posts[ids].body}</p>
</blockquote>
</div>
</div>
</br>
`
}
});
document.querySelector(".result").innerHTML = html;
}
}
xhr.send()
}
function getAll() {
var url = "https://jsonplaceholder.typicode.com/posts";
var xhr = new XMLHttpRequest;
xhr.open('GET', url, true);
xhr.onload = function () {
if (this.status === 200) {
var posts = JSON.parse(this.responseText);
var html = '';
posts.forEach(post => {
html += `
<div class="card">
<div class="card-header">
${post.title}
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>${post.body}</p>
</blockquote>
</div>
</div>
</br>
`
});
document.querySelector(".result").innerHTML = html;
}
}
xhr.send()
}