Joseph Mckenzie Joseph Mckenzie - 1 year ago 98
Javascript Question

How to change my js function from on click of enter to when i click submit button

I have a js script that I am extending to meet my needs as I'm still pretty new to JavaScript and have tried a few things but cant seem to get them to work.Here is part of JavaScript script if more is needed I can put more of it up.What I would like to do is have it work exactly the same way but instead of hitting the enter key i want to have the user click submit (or a button that looks like submit would work too) with their mouse.

(function () {

'use strict';

var ENTER_KEY = 13
var auditor = document.getElementById('auditor');
var date = document.getElementById('date');
var location = document.getElementById('location');
var workers = document.getElementById('workers');
var contact = document.getElementById('contact');

Where ENTER_KEY gets used (for second time)

function newTodoKeyPressHandler( event ) {
if (event.keyCode === ENTER_KEY) {
auditor.value = '';
date.value= '';
location.value = '';
workers.value = '';
contact.value = '';
company.value = '';
exposureLocation.value = '';
workersExposed.value = '';
exposures.value = '';
interventions.value = '';
interventionComments.value = '';
additionalComments.value = '';
programManagement.value = '';


If anyone could help to accomplish this I would greatly appreciate it. I already know ill have to change event.keyCode to something else but the things I tried were to no avail. Thanks

Answer Source

So if I understand correctly, your newTodoKeyPressHandler is being invoked on press of a keyboard button. Basically, you need to define an event handler for mouse click and then add a listener for the same as follows:


// mouseClickHandler Function

 function mouseClickHandler(event){
     ... (handler code, same as in the keypress handler)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download