Validate form for radio group

Hello Experts,

I am currently working on developing a form that includes a JavaScript validation function for a radio group. The main objective is to have a division pop up when either "Monday" or "Tuesday" is checked, and no popup when "None" is selected:-

//the form
<form name="form_sf" id = "form_sf" method="post" action="process.php" onSubmit="return check_sf_form_info(form_sf,'all')">

<input name="date" id="date" type="radio" value="nil" onChange="check_sf_form_info(form_sf,2)" />None

<input name="date" id="date" type="radio" value="mon" onChange="check_sf_form_info(form_sf,2)" />Monday

<input name="date" id="date" type="radio" value="tue"  onChange="check_sf_form_info(form_sf,2)" />Tuesday

</form>

//the javascript

function check_sf_form_info(form,mark,edit){
        if(mark==2 || mark=="all"){
     if(form.date.value!=="nil"){
       price.innerHTML="$100";
       price.style.display="block";
       price.style.height="auto";
     }else{
       price.innerHTML="";
       price.style.display = "none";       
     }
   }
}

I would appreciate it if experts could kindly point out any mistakes in my code. Thank you very much!

Answer №1

The radio button labeled as "nil" is the initial option in the group of radio buttons, allowing you to utilize the 'getElementsByName' function.

function validateForm(form, mark, edit){
     var radio_date_nil = document.getElementsByName('date');
     if(mark == 2 || mark == "all"){
        if(radio_date_nil[0].value !== "nil"){
            price.innerHTML = "$100";
            price.style.display = "block";
            price.style.height = "auto";
        } else {
            price.innerHTML = "";
            price.style.display = "none";       
        }
    }
}

Answer №2

My recommendation would be to update all of your onchanges to

onChange="check_sf_form_info(this,2)"
and then modify your function as follows:

function check_sf_form_info(radio,mark,edit){
   if(mark==2 || mark=="all"){
     if(radio.value!="nil"){
       price.innerHTML="$100";
       price.style.display="block";
       price.style.height="auto";
     }else{
       price.innerHTML="";
       price.style.display = "none";       
     }
   }
}

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

Ways to conceal an animated gif once it has been downloaded?

Is it possible to have an animated gif image vanish once server-side Java code runs and the client receives an HTTP Response from the webserver without relying on Ajax? I am currently utilizing the following Struts2 submit button: <s:submit value="sho ...

Using Ajax to implement Basic Authentication for securely accessing an https-protected webpage without requiring users to manually enter their username and password in a

Is there a way to access and display a website page hosted at this URL - , without encountering any authentication dialog box from my application on the same network? I have been unable to bypass the username and password entry request. I successfully imp ...

Giving identification to a pair of elements located within the same column

Struggling with assigning IDs to two elements in a single column - a dropdown and a text element. Managed it in the first scenario, but encountering issues in the second one. Seeking assistance on this matter. Scenario 1: <td> <sele ...

Utilize a function or array to send various data with an Ajax post request

Hey, I'm on the hunt for a more efficient method to send data using ajax to php for multiple users. Take a peek at my code below: $(document).ready(function(){ $("#all").click(function(){ document.getElementById('babon').click(); ...

Toggle visibility (individually duplicating) one thousand sentences while maintaining optimal browser and host performance

I have created JQuery scripts to toggle the display of future sentences on my page (approximately 1000 duplicates) and I am seeking the most efficient method (for browser/host) to accomplish this task. Here are two functional solutions that I have already ...

Using Vue.js to dynamically append varying inputs upon user interaction

When I select an option, I want to display different types of inputs based on the selected option. For Example: Select input -> show input fields Select textarea -> show text areas Select boolean -> show radio buttons The Code This is where ...

What sets apart the $ and $() functions in jQuery?

After reviewing the jQuery API, I noticed that $() represents a collection of matched elements. However, I am curious about the significance of $. An example from the imagesLoaded library is provided below. if ( $ ) { $.fn.imagesLoaded = function( opt ...

"Uncovering the parent element with jQuery: A step-by-step guide

I need help increasing the font size of a parent element without affecting the entire body's font size. How can I achieve this? For example, with the id="vs-1", I want to increase the font size of the grandparent li text here which is "1940". $("li ...

Is there a way to obtain the tasklist result of exec() from child_process and convert it to JSON format?

When I use the tasklist command with child_process, the stdout returns processes in Unicode string format that is not easily queryable. Here is the code snippet: var exec = require('child_process').exec; ... exec('tasklist', function( ...

A Step-by-Step Guide to Mocking a jQuery Function Using Jasmine in an Angular Directive Specification

When working with an angular directive, I am currently using $(element).fdatepicker(). How can I mock or stub this function in a jasmine test for the directive? If I don't stub it, I encounter the following error: TypeError: 'undefined' is ...

Having difficulty accessing the sound file despite inputting the correct path. Attempts to open it using ./ , ../ , and ../../ were unsuccessful

While attempting to create a blackjack game, I encountered an issue. When I click the hit button, a king card picture should appear along with a sound. However, the sound does not play and the error message Failed to load resource: net::ERR_FILE_NOT_FOUND ...

Incorporating a Link into a Radio Button component in Material-UI using react-router

Greetings! I have two radio buttons and would like to include a link. I attempted to achieve this in the following manner: <RadioButton value="/searchByArtistAndName" label="Artist and Name" style={styles.radioButton} contai ...

Press on the row using jQuery

Instead of using link-button in grid-view to display a popup, I modified the code so that when a user clicks on a row, the popup appears. However, after making this change, nothing happens when I click on a row. Any solutions? $(function () { $('[ ...

Tips for transferring information from ng-view to the navbar on the index.html page using AngularJS

Recently, I embarked on a journey to learn the MEAN stack and decided to challenge myself by building an authentication page from scratch instead of using the out-of-the-box solution. My main struggle lies in updating texts on my navbar. Here's a snip ...

What is the best way to create a circular to square gradient and save it within a two-dimensional array?

Can anyone guide me on creating a circle/square gradient and storing the data in a 2D array? I want to incorporate this gradient with simplex-noise to develop a procedural island by subtracting the gradient from the generated noise. Here are some visual re ...

Errors that occur when parsing templates in Angular 2

I'm encountering a template parse error with my Angular 2 component using the following template: <div *ngIf="chapter == 1"> <p><h4><br><b>Exercise</b>: Get the Groceries starting point<br></h4 ...

Neglect variables that have not been declared (TypeScript)

Currently, I am working on developing a WebExtension using TypeScript that will be later compiled into JavaScript. This extension relies on one of the browser APIs offered by Firefox, specifically the extension API. An example of this is my use of the get ...

Instead of displaying a regular text box, the output unexpectedly shows "Printing [object HTML

There are some HTML and JavaScript files that I have. I've written some functions in my JavaScript file to save the values of different input fields. However, when I try to print the description, it just displays [object HTMLInputElement]. This mak ...

Incorporate an assortment of facial features into BufferGeometry using three.js

If I have a BufferGeometry, I can easily assign its vertices using an array of type Float32Array with the following code snippet: geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); However, is there a way to set the f ...

Reorder elements in CSS Grid

I've been working with a css-grid and it's almost there, but I'm stuck on reordering the items in a specific way. Currently, the html layout is set as “1, 2, 3, 4,…10”, but for smaller screens, I want the visual order to be “1, 2, 4 ...