Can we display the chosen form values before submitting?

Want to implement a confirmation message for users before submitting their form using

onClick="return confirm('are you sure ?')"
.

The basic form structure is as follows:

<form>
<Select name='val[]' class='select'><option></option><option value='a'>a</option><option value='b'>b</option><option value='c'>c</option><option value='d'>d</option></select>
<Select name='opt'><option>AAA</option><option>BBB</option><option>CCC</option></select>
<input type='submit' name='submit' value='submit' onClick="return confirm('are you sure ?')">
</form>

If the user clicks submit, how can I update the confirmation message to display the values they have selected from the dropdown menus?

Edit:

This page is utilizing the Jquery Chosen script available at

The first option is a multiselect dropdown and styled using chosen.

The suggested solution provided by dishwasherWithProgrammingSkill works but does not show the multiple values selected.

Is there a way to achieve that functionality?

Update: Using : var opt3=$('#opt1').val();

The above code returns a comma-separated list. How can I remove the commas?

Appreciate any help. Thank you!

Answer №1

It seems like this could be the solution you've been searching for.

<form>
    <select name='val' class='select' id='opt1'>
        <option></option>
        <option value='a'>a</option>
        <option value='b'>b</option>
    </select>
    <select name='opt' id='opt2'>
        <option value='AAA'>AAA</option>
        <option value='BBB'>BBB</option>
    </select>
    <input type='submit' name='submit' value='submit' onClick="return function1();">
</form>

<script type='text/javascript'>
    function function1(){
        var opt1=document.getElementById("opt1").value;
        var opt2=document.getElementById("opt2").value;
        var response=confirm("Are you sure? option1="+opt1+" option2="+opt2);
        return response;
     }
</script>

UPDATE

Having already acquired the values using var opt3=$('#opt1').val();, the next steps are straightforward. You can utilize the split() function in JavaScript, similar to PHP's explode() function. Here is an example:

var opt3=$('#opt1').val();
var valArray=opt3.split(",");  //Specify where you want to split
for (var i = 0; i < valArray.length; i++) {
    alert(valArray[i]);
}

Answer №2

To achieve this, you will need to create a custom function. With the help of jQuery, you can utilize $(form).submit(function(){}); to retrieve your values when the form is submitted, display a confirmation dialog, and set the content accordingly.

I would be interested to hear about your progress!

http://api.jquery.com/submit/

Answer №3

To enhance the functionality of your form, it is recommended to separate your JavaScript code and apply an event listener. However, if you prefer using the onclick attribute, consider assigning an id to each form element and retrieving their values with getElementById():

<form>
    <select name='val[]' id='val' class='select'><option></option><option value='a'>a</option><option value='b'>b</option><option value='c'>c</option><option value='d'>d</option></select>
    <select name='opt' id='opt'><option>AAA</option><option>BBB</option><option>CCC</option></select>
    <input type='submit' name='submit' value='submit' onClick="return confirm('are you sure? val='+getElementById('val').value +' opt='+getElementById('opt').value)" />
</form>

For a practical demonstration, refer to this jsFiddle example - http://jsfiddle.net/rpcBS/

Answer №4

Make sure to use onSubmit() instead of onClick() when listening for form submissions.

<form onSubmit="return confirm('Are you sure?);">
    <select name='val[]' class='select'><option></option><option value='a'>a</option><option value='b'>b</option><option value='c'>c</option><option value='d'>d</option></select>
    <select name='opt'><option>AAA</option><option>BBB</option><option>CCC</option></select>
    <input type='submit' name='submit' value='Submit'>
</form>

Using onClick will not prevent the form from being submitted.

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

Having trouble getting JavaScript to select a stylesheet based on time?

I am attempting to implement two different styles based on the time of day. Here is the code I am using to select the CSS file depending on the current time: <script type="text/javascript"> function setTimedStylesheet() { var currentTim ...

The issue with Three.Js Raycasting arises when attempting to change camera transformations within an Object3D parent element

In my latest project, I decided to create a "camera controller" that handles the movement and rotation of the camera by utilizing an Object3D as its parent. The Y-axis rotations are applied to the Object3D, while the X-axis rotation is directly applied to ...

Utilizing Ajax and jQuery to extract information from an XML file based on an identification number

Currently, I am looking for a way to showcase a specific product with its description from my XML file by using the product's ID. The structure of my XML file is shown below: https://i.sstatic.net/wFWgW.png Below is the JavaScript code I am using: ...

Transforming a two-digit year into a four-digit year

Our entry form provides users with a calendar drop-down option, but they are also able to manually type in dates. We recently discovered that if someone enters a 2-digit year, it automatically changes to 1912 instead of 2012 (as dates cannot be in the past ...

The challenge with the Optional Chaining operator in Typescript 3.7@beta

When attempting to utilize the Typescript optional chaining operator, I encountered the following exception: index.ts:6:1 - error TS2779: The left-hand side of an assignment expression may not be an optional property access. Here is my sample code: const ...

JSON has encountered an unconventional string at position 41 that was not anticipated

Attempting to learn Javascript as a beginner, I am facing an error that is puzzling me: SyntaxError: /home/runner/myrepl/config.json: Unexpected string in JSON at position 41 Upon checking my index.js file, it seems correct at position 41 (at least I beli ...

Combining a group of JavaScript objects

I am facing a challenge with my collection as I need to perform aggregation using only JavaScript. I have attempted various approaches utilizing the Lodash library but unfortunately, I have not been successful. If you could provide me with some guidance on ...

Creating Table Rows on-the-fly

Seeking assistance and guidance from the community, I have modified code from an online tutorial to allow users to dynamically add rows to a table when data is entered in the row above. However, I am facing an issue where I can only insert one additional ...

Issue with process.env.NODE_ENV not functioning appropriately in NodeJS when utilizing package.json scripts

I currently have three separate databases configured for testing, development, and production purposes. My goal now is to make my express app switch between these databases based on the script that is being executed. These are the scripts I am using: "s ...

Shuffle letters in a word when hovered over, then unscramble them back to their original order

I have noticed a fascinating effect on several websites. To give you an idea, here are two websites that showcase this effect: Locomotive and TUX. Locomotive is particularly interesting to look at as the desired effect can be seen immediately when hovering ...

Deleting images from Firestore using a Vue.js componentReady to learn how to remove images

I recently came across a Vue.js image uploader component that I am trying to repurpose (https://codesandbox.io/s/219m6n7z3j). This component allows users to upload images to Firebase storage and save the downloadURL to firestore for continuous rendering of ...

What makes using $(selector).post() and $.get() in jQuery AJAX so valuable?

While browsing w3school, I came across a discrepancy in the definition of AJAX post and get. Post: $(selector).post(URL,data,function(data,status,xhr),dataType) Get $.get(URL,data,function(data,status,xhr),dataType) Why is there a post method with sel ...

Alignment of Bootstrap thumbnails in a horizontal layout

After adding the thumbnail class to my thumbnails div, I noticed that some of the thumbnails were smaller in size than others. Wanting them all to have the same height, I decided to give each thumbnail a fixed height of 210px. However, this caused the sm ...

How can I extract the text within an element based on the position of the

In my current project, I have incorporated the incredibly useful jQuery TextRange plugin. Within a highlight div positioned above a textarea element, it is crucial for me to identify the specific element that the user is currently editing. To better illust ...

How can you utilize a bind function to deactivate a button?

Can someone help me figure out how to temporarily disable a button using the bind function for 10 seconds? jQuery('#wsf-1-field-155').bind('click', function() { ScanRegistration(); setTimeout(function() { jQuery('#wsf- ...

"Internet Explorer naturally selects the submit button when users press the enter key to submit a

In my current application, I have implemented a form with a hidden button to address issues with the numeric keyboard on Android. Essentially, pressing enter or focusing on the invisible button will trigger form submission. Pressing enter works fine in Ch ...

Encountering a "Vue is not defined" error in Laravel 5.8 while constructing a comment system using Vue.js

I'm struggling to implement a comment system using Vue.js in my Laravel project. Despite my efforts, I keep encountering a "Vue not defined" error in the console. Can anyone shed some light on why this might be happening? Below is the code snippet I&a ...

Facing difficulty transferring an array from React to Django

Trying to transfer an array from the React frontend (stored in local storage) to my view class in Django is resulting in the following error: Console Output: GET http://127.0.0.1:8000/api/quiz/multiple/ 500 (Internal Server Error) Django Logs: for qu ...

Horizontal navigation bar encroaching on slideshow graphics

I'm encountering difficulties aligning the horizontal menu below the image slider. When I have just an image without the slider, the menu aligns properly (vertically), but as soon as I introduce the code for the slider, the menu moves to the top and d ...

"Enhance your client-side PDF capabilities with the pdfMake plugin, offering support for multiple languages

I am in search of a plugin that can convert HTML content into a PDF format and is compatible with javascript/jQuery/AngularJS. It must also provide multilingual support. I have experimented with the following two plugins, but encountered limitations with ...