You cannot use the "this" keyword outside of a class body

I am facing an issue with my function, can someone help me? Here is the code:

function remove_multi_leg(): void  {
  if (Number($("#search_no_legs").val()) < 2) {
    return;
  }

  const removeId: number = Number($(this).attr("data-number"));
  const highestId: number = Number($("#search_no_legs").val());

  if (removeId === highestId) {
    $(`#multi_leg_${removeId}`).hide();
    $("#search_no_legs").val(highestId);
    return;
  }

  let i: number;
  let end: number;
  let asc : boolean;
  for (i = removeId, end = highestId, asc = removeId <= end; asc ? i <= end : i >= end; asc ? i += 1 : i -= 1) {
    $(`#search_legs_${i}_origin_text`).val($(`#search_legs_${i + 1}_origin_text`).val());
    $(`#search_legs_${i}_origin_id`).val($(`#search_legs_${i + 1}_origin_id`).val());
    $(`#search_legs_${i}_destination_text`).val($(`#search_legs_${i + 1}_destination_text`).val());
    $(`#search_legs_${i}_destination_id`).val($(`#search_legs_${i + 1}_destination_id`).val());
  }
  $(`#multi_leg_${highestId - 1}`).hide();
  $("#search_no_legs").val(highestId - 1);
  return;
}

I encountered a strange error while running Codacy analysis on this code. Here are the details:

the "this" keyword is disallowed outside of a class body

The error seems to be in this line:

const removeId: number = Number($(this).attr("data-number"));

Can anyone suggest how I can resolve this issue?

Answer â„–1

To make the function more efficient, you can modify it to avoid using this by accepting an argument and utilizing the argument's currentTarget property like this:

function update_single_item(event: Event): void {
   const target = $(event.currentTarget);
   // ...
   const itemId: number = Number(target.attr("data-id"));
   // ...
}

(The parameter type for event could be specified as MouseEvent, especially for a click event, which includes x and y properties.)

currentTarget and this typically point to the same element in a DOM event handler; it represents the element where the event was triggered (in contrast to event.target, which might be a child of the targeted element).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn the process of eliminating a class utilizing this JavaScript function

This script is designed to identify and manipulate elements with the class menu-option-set. When an element within this class is clicked, it adds the class "selected" to that specific element while removing it from all others in the list. My goal is to en ...

"Typescript throws a mysterious 'Undefined value' error post-assignment

I'm currently working on a task to fetch my customer's branding information based on their Id using Angular. First, I retrieve all the customer data: this.subscription = this.burstService.getBurst().subscribe(async(response) => { if (r ...

Discovering the functionality of detecting the pressing of the "enter" key within an input field that triggers the next button in Internet Explorer versions 8 through 10

My current challenge involves detecting Internet Explorer's behavior when the Enter key is pressed in an input box with a button element beside it, especially when they are not enclosed within a form element. This unique feature of IE is intriguing b ...

Methods used on a server and methods used on a client-side application

In my ASP.NET application using C# 2.0, I have created objects for a database with properties that can be called natively or through a RESTful JSON API. These objects are used to convert data into HTML for display on the website. On the site, there are se ...

Upon clicking, choose a specific todo ID

I'm currently in the process of developing a basic todo application, and I am receiving data through props from a GraphQL Server in the following format: id:"5b3447a7b9d0e02144538972" text:"biibbb" My current issue is that when I click on the checkb ...

Efficient method for quickly updating the color scheme of a grid of squares in JavaScript

Currently, I am developing a JavaScript game that features a 2D array of squares with background colors that update each frame. My current approach involves utilizing a borderless DOM table and setting the style.backgroundColor property of each cell every ...

How can I prevent the sidebar from sticking to the footer?

I've implemented a floating widget in my sidebar using jQuery. You can check it out on my post Fat Loss Factor Review - as you scroll down, you'll see what I mean. I'm looking for assistance with stopping my AdSense widget in the sidebar bef ...

Tips for Creating JavaScript Code for HTML Helpers in ASP.NET MVC

My HTML helper includes a textBox that looks like this: @Html.TextBox("txt1") <br /> I am interested in triggering a javascript onchange event on this textbox. Can this be accomplished, or would it be better to use an HTML input type instead? ...

Is there a way I can turn off the dragging functionality in my situation?

Is there a method to disable the dragging functionality within a draggable element? $('#dragitem').draggable({ scroll : false, drag: function(event, ui){ //check if condition is met if(†...

Issue with second button in Angular Onclick() not been resolved

I'm experiencing an issue with the onClick() methods in my code. There are two onClick() methods present on the UI, but when I click on the second one, it does not call the method while the first one does. I am struggling to figure out why this is hap ...

What is causing this JavaScript function to output '2'?

Recently, I came across an unusual JavaScript function: (function f(){ function f(){ return 1; } return f(); function f(){ return 2; } })(); To my surprise, it returns 2 instead of crashing the browsers as expected due to recursion. Curious ...

How to Condense an Element Using Position: Absolute

https://i.sstatic.net/GMUss.png I'm attempting to create functionality where the "Open Chat" button displays an Absolute positioned div, then hides it when clicked again. I experimented with using the react-collapse component, but encountered issues ...

I encountered an issue while attempting to include Access-Control-Allow-Origin in my WCF library Project

Can someone help me figure out why this ajax call isn't working as expected? $.ajax({ type: 'GET', url: "http://localhost:8732/Design_Time_Addresses/InMotionGIT_NT.Address.Service/AddressService/json/capitalize", da ...

Enhance user experience with a dynamic Bootstrap combo box that updates based on

I am currently facing an issue with the bootstrap combobox plugin. I am having trouble changing the selection and sending that information from the view to the controller. $('#MyCombo').on('change', function () { var data = $(this) ...

What benefits are there to converting a jQuery application to Angular?

If I have a basic landing page with jQuery effects that I want to upgrade by "angularizing" it with AngularJS, how could I do so? For example, when using AngularJS for a contact form, it's known to be simple to implement. When considering DOM effects ...

CdkVirtualFor does not display any content

I'm facing an issue with implementing cdk-virtual-scroll in my chat application. Unfortunately, it's not showing anything on the screen. Strangely, when I resort to using the regular "ngFor", everything works smoothly. However, as soon as I switc ...

Shut down jquery modal

I have successfully imported Jquery Modal using the following two links (js and css) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cl ...

JavaScript Navigation Bar Error: The value of 'undefined' is not recognized as an object

Having just started learning HTML, CSS, and JavaScript, I encountered an error message that reads: TypeError: 'undefined' is not an object. Despite my best efforts to troubleshoot the issue, I have been unable to resolve it. Is there anyone who c ...

Converting JSON data into a Backbone Model

My current project involves utilizing backbone.js, and I have encountered a json data structure as follows: { first_name: 'David', last_name: 'Smith', family: [{father: 'David', mother: 'Rose', brother: ...

Tips for modifying the background color of all elements ahead of the element I have selected within a grid

https://i.stack.imgur.com/cW4Lp.png I'm trying to achieve a functionality where clicking on the 20th numbered block changes everything before it to light orange. I have a sandbox code snippet attached and would appreciate any guidance on what needs t ...