Steps to display a specific div section upon selecting a dropdown option and clicking the submit button using jQuery

I'm having an issue displaying a specific div section when selecting an option from a dropdown menu. I've written the code below, but it doesn't seem to be working. Can anyone please assist me with this? My actual requirement is that I only want to show the particular div section when I select an option from the dropdown and then click the submit button.

 

        function checkTenant(event) {
            event.preventDefault();
            var drpdownvalue = document.getElementById("tenentval").value;
            
            alert(drpdownvalue)
            if (drpdownvalue.value == "one" || drpdownvalue.value == "two") {
                //alert(drpdownvalue)
                document.getElementById("ifYes").style.display = "block";
                //alert(drpdownvalue)

            } else {
                document.getElementById("ifYes").style.display = "none";

            }
        }
    
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2d20203b3c3b3d2e3f0f7b6179617d">[email protected]</a>/dist/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.6.3.js"
        integrity="sha256-nQLuAZGRRcILA+6dMBOvcRh5Pe310sBpanc6+QBmyVM=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a3bca3a3b6a1fdb9a093e2fde2e5fde2">[email protected]</a>/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c6cbcbd0d7d0d6c5d4e4908a928a96">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">

                    <select class="form-control" id="tenentval">
                        <option>Select</option>
                        <option value="one">1</option>
                        <option value="two">2</option>

                    </select>
                </div>
            </div>

            <div class="col-md-2">
                <button type="button" onclick="checkTenant(event)" class="btn btn-primary">Submit</button>

            </div>

        </div>


        <div class="row div1" style="display: none;" id="ifYes">
            <div class="col-md-12">
                <div class="div1" style="width: 100px; height:100px; border:1px solid #f00"></div>
            </div>
        </div>
    </div>


   




</body>

</html>

Answer №1

The issue lies in the fact that the condition is never satisfied because the value is already obtained on

var drpdownvalue = document.getElementById("tenentval").value
.

Therefore, when attempting to access drpdownvalue.value, it will always result in undefined.

if (drpdownvalue.value == "one" || drpdownvalue.value == "two")

Simply utilize

if (drpdownvalue == "one" || drpdownvalue == "two")

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

How can I display an image caption within a lightbox using jQuery?

There is a caption under each image. <p> <a href="large.jpg"> <img width="85" height="75" src="thumb.jpg/> </a>Caption</p> I am looking to display the same caption at the bottom of the lightbox. <span id="lightbox-image ...

Angular is throwing a RangeError due to exceeding the maximum call stack size

Encountering a stackOverflow error in my Angular app. (see updates at the end) The main issue lies within my component structure, which consists of two components: the equipment component with equipment information and the socket component displaying conn ...

Sending an object along with additional variables back to the client through JSON serialization: Tips and Tricks

What is the best way to incorporate the dictionary a into the response, and how can I retrieve two objects through an ajax call? view def abc(request): cp = Cp.objects.get(id=1) cp = serializers.serialize('json', [cp,]) cp = json.lo ...

Exploring the power of Ajax within the MVC framework

I'm attempting to incorporate a view through Ajax, but I'm encountering some issues. My knowledge of Ajax is limited, but I am eager to learn. What could be the missing piece here, or am I completely off track with my approach? foreach(ect.. ...

Detailed breakdown of the code snippet below

Currently, I am diving into the world of Mongodb and finding myself a little perplexed by this particular line of code: mongoose.connection.once('open',function(){ console.log('connection acquired harsh bajpai '); }).on('error&a ...

React Hook for modifying a value within an array of objects in real-time

Is there a way to update the score of each player with a simple click? I have already implemented the initial code and all that's left is to define the function that will trigger the score update for each user. The increment value is +1. const [p ...

Attempting to have the command "npm run dev" generate two separate shell instances

For my nuxt project, I decided to use json-server as the local server. My goal is to automate the process of launching the server and running the project on a separate shell instance by using the command "npm run dev". After some exploration, this is the ...

What steps can be taken to provide accurate information in the absence of ImageData?

Utilizing a JS library to convert a raster image into a vector, I encountered an issue where the library returned a fill of solid color instead of the desired outcome. I suspect that the problem lies within the ArrayBuffer. The process of loading an imag ...

What is the best way to retrieve distinct id values from a form in Rails when sending a DELETE request via AJAX?

I am determined to grasp the intricacies of AJAX and how it operates, hence, I prefer not to use "remote: true" for this task. The issue at hand is that despite attempting to send a DELETE request to the server using a form input, each submission results i ...

Adaptable Semantic UI form design

Welcome, internet friends! If anyone out there has a moment to spare and is familiar with Semantic UI, I could really use some assistance... Currently, I am working on a form that looks great on larger screens like this: https://i.stack.imgur.com/cafc5.j ...

"Exploring the density of the xAxis in Highcharts

Currently, I am incorporating Highcharts into my jsp. Typically, when using Highcharts, we are able to create charts with all points evenly spaced along the x-axis. However, in this instance, I am interested in setting the points based on their x-values ...

Efficiently passing multiple files with Rails using jQuery/Ajax and API

I'm currently utilizing the FileStack API along with the filepicker gem in my project. The JavaScript snippet provided below is responsible for parsing a JSON response from the browser and forwarding it to the create action of the Attachment controlle ...

How can we verify the validity of URLs based on their length, presence of capital letters, and specific whole words?

I'm currently working on a piece of code that verifies the URL provided by users during sign-up for my application. I want to implement the following restrictions: URL must be at least 3 characters long No capital letters allowed Avoid certain words ...

Utilizing base64_encode versus json_encode to pass encoded data through a hidden input

I am currently working on a project where users will be able to create live blocks using Ajax. I have encoded the block's elements map inside a hidden input's value and plan to decode it within the php file. My main question is this: I am utiliz ...

Show the GitHub repositories of the user within a React application

Even though I am relatively new to React, I managed to create a GitHub search application. In this app, you can input a user's name in a search box and view their avatar, bio, username, etc. However, I'm facing an issue with fetching their reposi ...

Ensuring Consistent Data in Web Forms When Editing a User's "Points" Field

In the process of creating a booking system, I am faced with an issue regarding how points are allocated to users. An admin can assign points to a user through a form, which the user then uses when booking a slot. The problem arises when the admin opens th ...

Error: CSRF token not found or invalid. What is the procedure for transmitting a CSRF token from the frontend to the backend?

In my Django project, I have a task that involves sending a date string from the frontend to the backend. On the frontend, I am utilizing JavaScript's fetch method. async function getCustomerInDeliveryDate(deliveryDate : String) { con ...

Edit and protect an image added on a website using HTML

I am looking to develop a code snippet that enables users to upload an image, which will then be displayed with cropping and a watermark overlay that can be easily saved by right-clicking. My plan is to use Java/HTML for this online uploading functionali ...

Error in parsing JSON: An unexpected token < was encountered at the beginning of the JSON input, causing a SyntaxError at position 0 when parsing with

I need to transfer an array from a PHP file to JavaScript and save it in a JavaScript array. Below is the snippet of JavaScript code: xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 ...

Using CSS, I have rearranged the content of a single row (tr) in an HTML table into two separate rows

I am currently facing a challenge with rearranging my table due to width constraints. My goal is to apply Jquery functions to the table in order to extract all parameters related to a product, but I want to avoid having nested tr elements or splitting para ...