How to pass an element using TypeScript / jQuery

I am trying to determine the position and height / width of a DOM element through TypeScript / JQuery. Before TypeScript, I just did something like this:

        function ActOnClick(itemClicked, loadPath) {
            //sample code
            var v1 = itemClicked.offset();
            var v2 = itemClicked.width();
            var v3 = itemClicked.height();

        };

        $(document).ready(function () {

            $("#idCRM").click(function () {
                ActOnClick($("#idCRM"), "/Widgets/GetCRM");
            });
}

where idCRM is the "section" in my cshtml document (but it can be any div, etc.)

Now I'm trying to move this code to TypeScript, but what type do I expect in the ActOnClick function of jQuery $ ("# idCRM")? Right now I’m choosing “Element”, but a) it doesn’t reveal width, height, etc. And b) the call to ActOnClick is not executed due to $ ("# idCRM")!

/// <reference path="../../ScriptsTS/jquery.d.ts" />
function ActOnClick(itemClicked: Element, loadPath: string) {

    var v1 = itemClicked.offset();  <-- FAILS
    var v2 = itemClicked.width();   <-- FAILS
    var v3 = itemClicked.height();  <-- FAILS

};

$(document).ready(function () {

    $("#idCRM").click(function () {
        ActOnClick($("#idCRM"), "/Widgets/GetCRM");  <-- FAILS
    });
}
+5
source share
1 answer

If you need only that code, you can always use Anytypescript type , for example:

function ActOnClick(itemClicked: Any, loadPath: string) {
  var v1 = itemClicked.offset();
  var v2 = itemClicked.width();
  var v3 = itemClicked.height();
};

Typescript Any, . $("#idCRM") , JQuery, jquery.d.ts, :

function ActOnClick(itemClicked: JQuery, loadPath: string) {
  var v1 = itemClicked.offset();
  var v2 = itemClicked.width();
  var v3 = itemClicked.height();
};

offset(), width() height(), Object, number number .

+14

All Articles