-2

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()
}
  • VS Code detects the language based on the file's extension, that you are working with – TechySharnav May 26 '21 at 10:33
  • `And my code below doesn't work because it sees my code as TypeScript` Your code execution at runtime has absolutely nothing to do with what language VScode thinks it is at edit time. You are getting an error because `getElementById("postId")` is not found in the DOM, not because of anything VScode can do. – Jeremy Thille May 26 '21 at 10:46
  • https://mikefrobbins.com/wp-content/uploads/2016/08/vscode-defaults1a.png – Jeremy Thille May 26 '21 at 10:49
  • Does this answer your question? [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Jared Smith May 27 '21 at 01:24

1 Answers1

0

If your problem is that Visual Studio Code is detecting a wrong extension you can edit this in the bottom right. It will say "TypeScript" click on this and change it to the extension you would love to code in.

Here you can see where to press in Visual Studio Code

Dharman
  • 21,838
  • 18
  • 57
  • 107
Legacy
  • 26
  • 1