JavaScript code for the submit button to proceed or halt the form submission

Within my JSP file, I have the following code snippet:

<input type="submit" value="Transfer ULD" onclick="doSomething();" name="_eventId_transferULDTransition"/>

The doSomething() function mentioned above is a JavaScript method.

function doSomething() {    
    var transferType = document.getElementById('equipmentType').value;
    if(${uldSelector.baseLoadToUse.uldEquipType} == 02 && 
        (transferType == 01 || transferType == 13 || transferType == 07)){
        var r = confirm("Still want to tranfer Air Container?");
        if (r == true) {
            return true;
        } else {
            return false;
        }
    }
}

Within this method, there are around 4 to 5 conditions...

The desired functionality is for the action to continue when the user clicks 'OK' and to cancel when they click 'CANCEL'...

However, in practice, it seems to submit in both scenarios using Spring Webflow, Spring MVC, and Java.

I would greatly appreciate any suggestions or advice on how to address this issue! Thank you!

Answer №1

In order for the onclick function to work properly, it requires a return statement. For example:

<input type="submit" value="Transfer ULD" onclick="return doSomething();" name="_eventId_transferULDTransition"/>

It is common practice to include a return false within the function, but it's important to note that the onclick method itself does not actually return a value.

Answer №2

The click action does not have a return value assigned to it

onclick="doSomething();

It should actually be

onclick="return doSomething();

However, it would be more effective to handle this on the form level with onsubmit.

<form onsubmit="return doSomething(); >

Why handle it on the form? This is because the form can be submitted without clicking the button, for example by pressing the Enter key in a text box.

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

The JavaScript script to retrieve the background color is malfunctioning

I am currently working on developing a highlighting feature for an HTML table that will dynamically change the row colors on mouseover. Below is the code snippet I have been using, but it seems to be experiencing some issues. Any assistance would be greatl ...

AngularJS ng-show will not function properly with newly added DOM elements

I am encountering an issue with ng-show directives on dynamically added DOM elements in AngularJS. The directives work fine when the page initially loads, but new elements that are added later do not have their expressions evaluated. Is there a way to prom ...

Extracting live content from a website within a native Webview

Running an eCommerce website along with a simple mobile app for both iOS and Android that features a basic tab bar menu, including icons like a shopping cart, profile, refresh button, etc., as well as a Webview to display the website content. The App' ...

Unable to create a flawless circle using Canvas

No matter how hard I try, drawing a perfect circle seems impossible for me. Every time I attempt it, all I manage to create is an ellipse. Check out this code snippet I put together as an example: function canvasClicked(number) { var c = "canvas" + nu ...

Filtering data in Vue.js using JSON specifications

How can I display only the names in the data? I currently have cards filtered by cities <div v-for="item in stationCityData"> <div v-for="option in filteredCity" style="background:#eee;padding: 20px"> <!-- <div v-for="option ...

What is the best way to include a context in a JavaScript promise?

Figuring out JS Promises has always been a challenge for me. I'm aware this might be a basic question, but please bear with me. =) Looking at the code snippet below, my goal is to modify a property within the class containing this function (essentiall ...

Change the default direction of content scrolling in CSS and JavaScript to scroll upwards instead of

I am currently working on a website where the navigation bar spans the entire width and remains fixed in place. Below the navigation bar is a cover image, followed by the main content section. As you scroll down, the navigation bar covers the image from t ...

AngularJS application: the button requires two clicks to activate

In my attempt to develop a simple CRUD app using AngularJS, I encountered an issue. The initial step involves clicking a button that fetches data from a REST API and populates an array. This array is then iterated using ng-repeat to generate Bootstrap card ...

Is it possible for Skycons to show a duplicate icon?

My current issue involves integrating the JavaScript plugin "Skycons" with the Yahoo weather RSS feed. The problem arises when multiple days have the same weather forecast, as the plugin retrieves icons based on ID rather than class. This prevents me from ...

What methods exist for determining the current location or city of a user without requesting permission?

Is there a way to automatically capture the user's current location on our website without explicit permission, possibly using IP address or MAC address? Has anyone successfully implemented this method in the past? ...

What causes addEventListener to not return a value?

In this snippet of code: let rockClick = rockBtn.addEventListener('click', playRound.bind("rock", computerPlay(), false)); After using console.log(), the output is undefined. The purpose of rockBtn:const rockBtn = document.querySelecto ...

Enhancing class instantiation through JSON deserialization with custom arguments

In Jackson, you have the convenient option to specify custom constructors or static factory methods for creating a class and getting parameters from the JSON object. However, I am unsure of the best way to pass an additional constructor argument to a deser ...

Running shell commands through child_process in JavaScript

Is there a way to execute shell commands from the browser and display the result on the UI using child_process? I'm having trouble fetching the results from the command line asynchronously. Is there something I'm overlooking here? const exec = ...

Guide to Establishing a Connection to WCF Service using Ionic Project and AngularJS

Greetings, I am currently experiencing an issue tasked with connecting my Ionic project to a WCF service located on another PC (running a C# Application) within the local network. I have verified that the network connection between the PCs is functioning p ...

Issue with Vue JS component on blade template page

Having trouble loading a Vue component into a Laravel blade file? Despite making changes, the content of my vue file isn't showing up in the blade file - even though there are no errors in the console. Any suggestions on how to resolve this issue woul ...

Tips for testing nested subscribe methods in Angular unit testing

FunctionToTest() { this.someService.method1().subscribe((response) => { if (response.Success) { this.someService.method2().subscribe((res) => { this.anotherService.method3(); }) } }); } Consider the following scenario. ...

The robot will automatically update its own message after a designated period of time

I am facing an issue with my code where the bot is not updating its message after a specific time period defined by time.milliseconds * 1000. How can I make the bot edit its message after that duration? let timeout = 15000; if (author !== null && ...

Is there a way to prevent a user who is already logged in from accessing their account on a different browser or tab

My current stack consists of reactjs, nodejs, and redux for user authentication. I am utilizing aws cognito to handle the user authentication process. The main functionality of my app is uploading files from users to an s3 bucket. One of my goals is to p ...

Creating vibrant row displays in Vue.js: A guide to styling each row uniquely

I am not a front-end developer, so Vue and JS are new concepts for me. Currently, I am working on an application for managing sales. One of the components in my project involves displaying invoices in a list format. To make the rows visually appealing, I ...

Blockage preventing text entry in a textarea

To enhance the basic formatting capabilities of a textarea, I implemented a solution where a div overlay with the same monospace font and position is used. This approach allows for displaying the text in the div with different colors and boldness. However ...