What is the best way to utilize JavaScript in order to eliminate a tag when an option is chosen within an HTML form?

I'm in the process of designing a website for an artist. The website includes a contact form, and I'm currently working on a JavaScript script to remove the hidden class that I applied to the commission rules section. The goal is to make this section visible when the user selects the Commission option in the form. While I am fairly new to JavaScript, after reading through some documentation, it seems like using the HTMLOptionElement type is the best approach to achieve this with vanilla JS. Below is the form and my attempt at removing the element:

                <form action="action_page.php" id="contactform" method="post" action="FormToEmail.php">
                    <input name="name" type="text" class="feedback-input" placeholder="Name" />   
                    <input name="email" type="text" class="feedback-input" placeholder="Email" />
                        <select id="reason" name="contact-reason">
                            <option value="None" selected disabled hidden>Reason for contact</option>
                            <option value="Brand">Brand</option>
                            <option id="Comission"value="Comission">Commission</option>
                            <option value="Other">Other</option>
                      </select>
                      <a class="invis hidden center" href=""> Commission rules</a>
                    <textarea name="contact-message" class="feedback-input" placeholder="Comment"> </textarea>    
                    <input type="submit" class="button form-buttion" value="SUBMIT"/>
                </form>

I have attempted selecting by ID, tags, and various other selectors in my JavaScript code, as shown below.

var action = document.HTMLOptionElement("Commission")
if(action == true){
  element.classList.remove("invis");
}

Answer №1

When trying to determine if an option is selected, using document.HTMLOptionElement as a function may cause an error. The correct method involves checking the value of the element with the specified ID 'reason':

document.getElementById('reason').value === "Commision";

Another option is to remove the ID attribute from the option element altogether.

Therefore, the updated code would look like this:

if(document.getElementById('reason').value === "Commision"){
   element.classList.remove("invis");
}

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

Issue with Vue-Multiselect: Unselecting a group of pre-loaded values is not functioning as expected

My code: https://jsfiddle.net/bgarrison25/tndsmkq1/4/ Html: <div id="app"> <label class="typo__label">Groups</label> <multiselect v-model="value" :options="options" :multiple="true" group-values="libs" g ...

Creating a unique 3D model through specified coordinates with the use of THREE.js

I possess an array consisting of (x,y,z) vertices coordinates as well as a facets array that stores the indexes of vertices (taken from the first array) for each facet. Each facet may have a varying number of vertices. Is there a way to construct a unique ...

Can you explain the variance in these code snippets when implementing React's setState() function?

Can you explain the variance between these two blocks of code? this.setState((state)=>({ posts: state.posts.filter(post=> post.id !==postRemoved.id) })) versus this.setState((state)=>{ posts: state.post ...

Ensure to check the input file for null values before proceeding

My task involves working with an input file: Razor: @Html.TextBox("archivo", "", new { type = "file",id = "archivo" } Html: <input id="archivo" name="archivo" type="file" value=""> I am trying to detect if the value of the input is null when a b ...

The error message encountered while using webpack with TypeScript and React is: "Unexpected token React.Component

I am currently working on setting up a project using webpack, typescript, and react. I have implemented babel to transpile my typscript/react code. However, when starting the development server, I encountered the following error: Module parse failed: Un ...

What causes Next.js code lifecycle to run multiple times?

Recently, I dove into learning Next.js and encountered something peculiar. The issue revolves around this straightforward code snippet: import {useRouter} from 'next/Router'; import {Profile} from '../../components/Profile'; const Jobs ...

Changing a particular field value within an array of objects in Angular 4

I have a scenario where I created a class called security: class security { public id:number; public name:string; } Next, I initialized an array of security objects: let s:security[]=[{id:1,name:'Alex'},{id:2,name:'John'},{id:3,nam ...

Building a multi-form application with HTML5 validation and Vue.js

I am working on a vue.js form that consists of multiple steps and requires HTML5 validation to be simplified. Here is a snippet of the code: <template> <div> <div v-if="activeForm === 1"> <h2> ...

The selected jQuery plugin is not functioning properly within CodeIgniter framework

I recently downloaded the jQuery Chosen plugin to use the simple "multiselect" version on my website. I followed all the necessary steps and even copied and pasted the code into CodeIgniter. Despite my experience with jQuery, I am facing an issue where the ...

What is the method for accessing the value of variable "a" in the following code?

request(target, function (err, resp, body) { $ = cheerio.load(body); links = $('a'); $(links).each(function (i, link) { if ($(link).text().match('worker')) { var a = $(link).attr('href').toStri ...

When using Mongoose's findOneAndUpdate method, it will not update the document and will not throw an error

I have two documents <Users and Poles> stored in my MongoDB Cluster database. Essentially, a pole functions as a team of users. What I am aiming for is that every time a user wants to join a pole, their name will be added to the members array. To ach ...

issues with functionality in purecss drop down menu

I have been struggling to create a vertical drop-down menu using the purecss library without success. purecss.io/menus/ This is what my CSS code looks like: <div class="pure-menu custom-restricted-width"> <ul class="pure-menu-list"> ...

Can the default Bootstrap classes in the ExtLib application layout control be modified?

I am currently using the responsive Bootstrap theme in combination with the application layout control in extlib, but I have been unable to locate any documentation on whether it is possible to modify the predefined Bootstrap classes. In the example below ...

Unraveling encoded characters in URLs using Node.js

When I try to send a cookie back using res.cookie(JSON.stringify({bar: "baz"}), the value that I get in return is %7B%22bar%22%3A%22baz%22%7D. How can I decode this in a Javascript setting like node.js? ...

Introducing a novel class designed to leverage the attributes of another class

Is there a way to use the @extend feature in CSS to inherit properties from one class to another? Imagine having two CSS files loading on a page in this order: <link rel="stylesheet" href="/css/one.css" media="screen"> <link rel="stylesheet" href ...

Struggling with eliminating the # from a URL in AngularJS

I am working on a single-page application built with AngularJS. The URL structure I currently have looks something like this: .../Angular/index.html. However, when I click on a link within the page, the URL transforms to .../Angular/index.html#/addStudent. ...

What is causing the child table (toggle-table) to duplicate every time a row in the parent table is clicked?

After creating a table containing GDP data for country states, I've noticed that when a user clicks on a state row, the child table displaying district GDP appears. However, there seems to be an issue with the child table as it keeps repeating. I&apos ...

What could be causing the isAuthenticated function to return false in the local strategy of passport.js even after a user

Here is the code I am using for authentication with passport-local strategy: routes.post("/login", passport.authenticate("local"), (req, res) => { res.json(req.user); }); function ensureAuth(req, res, next) { console.log(req.isAuthenticated()) ...

Tips for avoiding page reload when submitting a form with form.submit() in ReactJs

Is there a way to avoid the page from refreshing when triggering form submission programmatically in ReactJS? I have attempted to use this block of code: const myForm = () => <form onBlur={(e) => { if(!e.relatedTa ...

I am facing an issue with the Ionic Framework where the Javascript function for JWPlayer only works after the page is reloaded. Can anyone help

I'm currently troubleshooting the use of JWPlayer for streaming videos in an Ionic app. However, I've encountered a problem. The player only seems to load when I refresh the page, rather than when I navigate through the list of videos. Here is ...