random0620
random0620

Reputation: 1696

jQuery not recognized in external javascript file

I am trying to move my jquery code form script tags inside html to separate javascript files but there is no code complete in the new file and the $ variable is not recognized as jQuery. The code runs fine though. In my index.js file which does not have code complete in visual studio:

$( function(){
        $("a").attr({href:"http://reddit.com"}).text("reddit.com")
        $(".header").css("color","red");
    }
);    

In my html file I loaded jquery before index.js which has the jquery code inside it:

<html>
<head>
    <link rel="stylesheet" href="/static/css/style.css">
    <script type="text/javascript" src="/static/js/jquery.js"></script>

    <script type="text/javascript" src="/static/js/index.js"></script>

    <title>Welcome</title>

    <h1 class="header">Welcome to the test site</h1>
    <a href = "http://www.google.com">Google.com</a>
</head>
<body>
    <input type="button" onclick="location.href='/login';" value="Login" />
</body>
</html>

Does anyone know why my index.js does not recognize the jQuery library and assign $ to jquery variable? Thanks and let me know if I need to clarify anything.

Upvotes: 1

Views: 594

Answers (1)

random0620
random0620

Reputation: 1696

Okay so all I needed to do was install with npm:

npm install --save @types/jquery

Upvotes: 1

Related Questions