Adding an event listener to detect left or right mouse clicks - using onclick doesn't capture right clicks

I'm currently in the process of applying for an Internship through this Internship Link

One thing that caught my attention right away is the issue with uploading or pasting a cover letter. When attempting to upload or paste a cover letter, it redirects me to the homepage of the job invite site Job Invite, preventing the submission of a cover letter. However, the upload resume feature works smoothly while the paste resume option encounters the same problem.
Does anyone have any suggestions on how to fix this and successfully submit a cover letter?

Although I'm not an expert in web development, as I'm applying for an engineering position, I'm trying to troubleshoot this issue. By inspecting the element linked to the upload cover letter function, I discovered:

onclick="jvAddAttachment2('jvcoverletter', 'qLY9Vfwx')

This function gets triggered upon clicking the button. To investigate further, I accessed the JavaScript file for this webpage: Inspect Element -> Sources -> *careers_8.js?v=303. I attempted adding a basic alert statement, following guidance from W3Schools, for debugging purposes. Here's the updated code:

function jvAddAttachment2(id, companyId){
    alert("I got here");
    ....
}

After saving the changes (ctrl + s), the Inspect Element console confirmed "Recompilation and update succeeded." However, when I clicked the link (via right-click and open new window), the alert box did not appear.
Does anyone know how to make the alert dialog show up? I've tried my best with the knowledge obtained from a single web development course haha.

Answer №1

If you want to trigger an action on right click, you should use the contextmenu event:

element.addEventListener('contextmenu', function() {
  // insert your code here
});

Avoid using inline JavaScript like onclick in your HTML. It is recommended to attach event listeners the proper way as shown above.

To reference an element in JavaScript, use

document.getElementById('the-id')
or similar functions like document.querySelector.

You can attach both left and right click events to an element like this:

// left click
myElem.addEventListener('click', myFn);
// right click
myElem.addEventListener('contextmenu', myFn);

If you prefer using inline JavaScript, you can do it like this:

<div onclick="myFn()" oncontextmenu="myFn()"></div>

I have provided a full demo showcasing both approaches:

var myElem = document.getElementById('my-element');
myElem.addEventListener('click', myClickFn);
myElem.addEventListener('contextmenu', myClickFn);

function myClickFn() {
  console.log('this is myClickFn!');  
}

function someFn() {
  console.log('this is someFn!');  
}
<div id="my-element" onclick="someFn()" oncontextmenu="someFn()">Left or Right click me!</div>

If you need to pass parameters to the function called on click, create an intermediary function that calls the target function with the specified parameters:

function myClickFn() { 
  myOtherFunction('some', 'params');
}

This approach helps avoid repetition when passing parameters to the function in different places.

Answer №2

Ensure you properly close your onclick string with a ":

onclick="jvAddAttachment2('jvcoverletter', 'qLY9Vfwx')"

Remember to use left click instead of right click.

Answer №3

 onclick="jvAddAttachment2('jvcoverletter', 'qLY9Vfwx')"

I noticed that the double quotation was missing.

view demo

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

Can a promise be safely resolved more than once?

In my application, there is an i18n service that includes the following code snippet: var i18nService = function() { this.ensureLocaleIsLoaded = function() { if( !this.existingPromise ) { this.existingPromise = $q.defer(); var deferred ...

"Implementing a consistent body border design on all pages using CSS for print media

Having an issue with my html page layout where I am unable to display a border on both printable pages. My current code only adds the border to the first page. Any suggestions on what I need to change in order to show the border on every printed page? Be ...

What is the best way to retrieve the parent element quickly using Jquery?

html: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all"><input class="spinner1 ui-spinner-input" id="q1" name="value" value="1" min="1" aria-valuemin="1" aria-valuenow="2" autocomplete="off" role="spinbutton"><a class="ui ...

What are the different applications of npm packages?

Is it possible to use npm packages in any Javascript runtime environment? I have experience using them in Angular and Node, but are they universally compatible across all environments? Edit: To those who downvoted this post, as a newcomer seeking assistan ...

Javascript navigation menu failing to accurately display all pages

As I continue to enhance my website at , I have encountered an issue with the menu functionality. The menu is dynamically generated through JavaScript, scanning a folder for pages and populating them into an array. While this system functions smoothly ove ...

Support for h264 in Windows Media Player across various versions of Windows operating system

Are there specific versions of Windows that are capable of playing h264 videos with Windows Media Player installed by default? Is it feasible to determine this support using Javascript? We currently deliver MPEG-4 video files through Flash, but some users ...

Angular 8 carousel featuring a dynamic bootstrap 4 design with multiple images and cards displayed on a single slide

I am currently working on a dynamic carousel that should display multiple cards or images in one row. Initially, I faced an issue with the next and previous buttons not functioning properly when trying to display multiple cards in one row. After some onlin ...

Maintaining Order with SCSS and Compass - Is it Possible?

Does compiled SCSS using Compass preserve the order of declarations? Can Compass guarantee the order of properties (assuming it's the way Compass operates that determines this)? This is mainly important when there are multiple definitions with the s ...

CSS Alignment in React with Material UI

Is there a way to align one button to the left while centering the other two? To see an example in Codesandbox, please visit HERE <DialogActions sx={{ justifyContent: "center" }}> <Button>Left</Button> <Button on ...

The log indicates that there are two distinct IP addresses associated with the user

I find that this question may be better suited for another Stack Exchange board, and I am open to migrating it there if needed. In the development of a web application, we record certain event information to assist in diagnosing any potential issues. One ...

Make sure to close all your tags properly in PHP

Within my <textarea>, I am giving users the ability to submit content. The specific tags I want to allow include <b>, <i>,<blockquote>, and <del>. In order to prevent any unclosed tags since this content will be displayed on t ...

How to display a PDF in a web browser using PHP

I have the following code block: header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="the.pdf"'); header('Content-Length: ' . filesize($file)); @readfile($file); It is working perfectly fin ...

Expansive Offspring Division stretching to the Entire Vertical Length of its Guardian Division

I'm attempting to achieve full coverage of the parent div section by my child div section. Take a look at the Example URL (specifically where the Canadian Flag Stand Up Paddle Boarders are located) towards the bottom: Essentially, when I set the widt ...

Every time I try to set up the MailChimp pop-up javascript, I encounter an Uncaught Error message saying that Bootstrap tooltips need Tether and an Uncaught ReferenceError message

Utilizing Wordpress and Understrap. Upon inserting the following code: <script type="text/javascript" src="//downloads.mailchimp.com/js/signup- forms/popup/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"> </script><script ty ...

The redirect link to Facebook Messenger is functional on desktop browsers but experiences difficulties on mobile browsers

Currently, I am facing an issue with redirecting from a webpage to an m.me/?ref= Facebook link that points to a Facebook Page. The redirection works smoothly on the Desktop Browser and opens the Facebook Messenger as expected. However, when attempting the ...

Adjusting the size and location of the current browser window using jQuery

Is there a way to modify the height, width, and position of the browser window using jQuery's document.ready() function? ...

JavaScript does not function properly when interacting with the result of a POST request made through $.ajax

Question: After including jQuery files and functions in index.php, do I also need to include them in select.php? To clarify my issue, here is the problem I am facing: Initially, I am trying to send data to select.php using jQuery's $.ajax. The data ...

Converting XML schema to JSON format using Node.js

I am attempting to loop through or access keys of a JSON object that was created from an XML object in node.js. Here is my current code: var fs = require('fs'); var parser = require('xml2json'); var xsd = require('libxml-xsd&apos ...

Looking to determine if a specific element is assigned multiple class names

Help needed! Can you tell me how to check if an element contains more than one classname using pure javascript, without using jQuery? For example: If #test has both the classnames "classA and classB", then { alert(true) } else { alert(false) } Here is ...

The Node.js Express server seems to be having trouble accessing static files

After successfully starting the express server, I encountered an issue when trying to load static files which resulted in an error message reading "Cannot GET /URL". The static files are located within a folder named "Login" and both the app.js and "Logi ...