Multiple event listeners on a single button

I'm working on implementing GATracking for a group of pages. I want to track both pageloads and clicks on specific buttons. My goal is to create code that is easily adaptable without the need for extensive rework. Some functions already have bindings, so using multiple bindings in jQuery is not ideal. Additionally, I prefer not to use inline function calls.

One approach I'm considering is creating an array based on IDs and fetching values when needed. Alternatively, I could explicitly call functions on buttons or divs with href attributes, triggering a separate function once the binding is complete.

I'm seeking a more universal solution that centralizes most of the coding in a single class for versatility across various applications. How should I proceed?

For example:

1)

<div id="submit" class="submitClass">
(onClick binding already exists and adding multiple bindings in jQuery is discouraged).

2)

<li><a href="/" onclick='GATracking(<param1>,<param2>);'>Home</a></li>
(repeating this pattern for different functions would be tedious and lack generality)

Thank you in advance

Answer №1

Add arguments to each occasion

<div id="submit" class="submitStyle">

Utilizing jQuery

let counter = 0;
$("#submit").on("click", function(){
  if(counter == 1){
    //Implement specific action for first click
  } else if(counter == 2){
    //Execute different action on second click
  } else if(counter == 3){
    //Another action for third click
  } else {
    //Default action for any other click count
  }
  counter+=1;
});

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

Error: The checkbox property 'checked' is read-only and cannot be assigned to

After applying a filter to the following checkboxes, I noticed that once the filter is removed, the checkboxes disappear. The reason for this behavior is clear to me. However, the issue arises when attempting to assign the checked value to my ng-model in A ...

At what point should I expect a promise in Protractor to be resolved?

Despite there being similar inquiries on this topic, I am struggling to comprehend them. Allow me to illustrate with an example, where my task is to click a button and verify the URL. Initially, I thought it should be coded as: element(by.id('butto ...

The sidebar stays fixed in place and doesn't adapt to varying screen resolutions

Check out my website at . I have a fixed, blue sidebar on the left side of the page to ensure its content is always visible. However, I'm facing an issue with smaller resolutions like 1024x768 where some bottom content is cut off. How can I adjust the ...

Looking to Query in Angular with Firebase?

In my TypeScript file, I have implemented a query to the Angular Firebase database. import { Component, OnInit } from '@angular/core'; import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/datab ...

Utilizing CSS to create a dynamic full-screen background slider for a static website

I am working on a static website and I am in need of creating a background slider with a fixed height of 587px. The images are 2000px wide, but I want the user to only see up to 1000px width of each image on their screen. The image should be static and not ...

Change the left position of the sliding menu in real-time

I am currently designing a website with a sliding menu feature. By default, the menu is set to -370px on the left, displaying only the "MENU" text initially. When a user hovers over the menu, it expands to the right, allowing them to select different menu ...

Showing a temporary marker while utilizing jQuery UI sortable container

Incorporating a sortable list of items using jQuery UI in a bootstrap layout has been successfully declared. $("#ReportContainer").sortable({ helper: 'clone', revert: 'invalid', }); Each draggable item represents a column size ra ...

Is it possible to integrate Firebase Storage into a TypeScript/HTML/CSS project without the use of Angular or React?

For my project, I am aiming to create a login and register page using TypeScript. Currently, my code is functioning well even without a database. However, I would like to implement Firebase for storing user credentials so that the login process becomes mor ...

Encountered an unexpected symbol < in JSON while implementing fetch() operation

I'm currently working on linking my React signup page to my Django API in order to automatically create a user profile in Django when a user signs up. Whenever I attempt to create a new user, I encounter this error in my console: Signup.js:33 ...

Videos embedded using the React HTML video tag are not automatically playing on mobile devices

I have implemented a jsx variable to insert a video into my html. Despite following the advice to include muted defaultMuted, and playsinline (which I have already done), the videos autoplay on safari, chrome, and firefox on my computer but not on mobile ...

Hiding or removing DOM elements with ng-bootstrap pagination

I am in the process of incorporating pagination into my project using ng-bootstrap pagination. In my HTML, I am combining an ngFor loop with the slice pipe to filter elements for display. <tr *ngFor="let bankAccount of bankingAccounts | slice: (p ...

Oops! We encountered an issue in locating the Entry Module. The error message is indicating that it cannot find the specified source file

https://i.sstatic.net/m6kKr.png Currently enrolled in a Udemy course focusing on Wordpress development, I encountered an issue while attempting to integrate Google Maps into a custom post type. This required updating a JavaScript file, however, running &a ...

Using NodeJS to Load jQuery

Currently, I have an HTML file that contains some embedded JavaScript. To transfer this file to the client, I utilize NodeJS with functions such as fs.readFile() and response.write(). However, I am encountering an issue where the HTML renders correctly b ...

Update the date format in jQuery's datepicker

Currently, I am utilizing the jQuery datepicker in my project. I have connected a Textbox to the Datepicker. My goal is to adjust the date format to showcase dates in the following style: Tuesday, 29 - Jan - 2013. Could you please guide me on how to achi ...

The requested resource at http://127.0.0.1:8000/storage/profiles/ could not be located (Error 404: Not

I have successfully implemented a feature on my website that allows users to upload their avatar picture using Vue.js and Laravel as the backend. The image is stored in the storage directory, and I can display it on the user's profile page using Vue.j ...

Struggling to align an image in the center of a div on top of another image

I am attempting to center a logo on top of a moving image that scrolls through (unfortunately, the movement isn't visible on jsfiddle). I have tried using <center> tags, but they do not work. Adding margin: auto; does not properly center the ima ...

Exploring ways to personalize the behavior and style of the material-ui Accordion component

There is a div container in gray with an accordion component containing only one AccordionSummary component. Below that is another div in blue. The initial view looks good (see pic #1). When the accordion is expanded: The AccordionDetails component is dis ...

Utilizing the input type file within an anchor tag to create a dropdown item

I am facing an issue while trying to incorporate the input type file within a dropdown item inside an anchor tag. Here is the code snippet causing the problem: <div className="dropdown-menu actions-text"> <a className="dropdown-item" href= ...

Guide to utilizing multiple forms in Django version 2.1

Apologies for the repetitive nature of my inquiries, but I have not found answers that address my specific issue with utilizing Django 2. Question: I am facing a situation where I need to perform two actions: one to display data in a table and another to ...

In Visual Studio, make sure to include a reference to AngularJS.min.js within another JavaScript file

In my AngularJS app, I am utilizing Visual Studio with separate folders. The AngularJS-min.js file is located in a different folder. My query is how can I correctly reference the AngularJS-min.js file in my app's JavaScript file to enable auto-suggest ...