JavaScript jquery selector in typescript

1

I'm learning to use typescript and I do not understand why it does not work

initHideCookies = (function (selector:string) {
    //Hide Cookies

    $(selector).click(function () {
        $(".m_cookie").hide();
    });
})

the $ (selector) is not recognized, and the DOM event of 'click' either. It is assumed that in typescript it accepts the javascript language, so I do not understand why that error comes out.

    
asked by Anuska 25.04.2017 в 15:00
source

2 answers

3

The $ symbol is specific to jQuery, not the JavaScript language itself.

jQuery is a library that provides an abstraction layer over JavaScript to facilitate many of its features. In order to use it, you need to import it, adding it in head of your document html .

For version 1.12 one possibility would be:

<script src="https://code.jquery.com/jquery-1.12.4.js" integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU=" crossorigin="anonymous"></script>
    
answered by 25.04.2017 в 16:30
1

I have already solved this problem. What I did was write the following through the terminal in my project (in my case, visual studio code has the terminal integrated):

npm install jquery --save
npm install @types/jquery --save-dev

and then do an imports where you are doing typescript:

import * as $ from "jquery";

and it worked.

    
answered by 08.05.2017 в 17:37