Display a div when the value of a dropdown list changes using JavaScript

I am trying to implement a feature where a div is displayed on the onchange event of a dropdownlist using JavaScript, but I keep getting an error message saying object required

Here is the ASPX code snippet:

<asp:DropDownList runat="server" ID="lstFilePrefix1" onchange="showTR();" >
    <asp:ListItem Text="Prefix1" Value="Prefix1" />
    <asp:ListItem Text="Prefix2" Value="Prefix2" />
    <asp:ListItem Text="Prefix3" Value="Prefix3" />
    <asp:ListItem Text="Prefix1 and Prefix2" Value="Prefix1 and Prefix2" />
    <asp:ListItem Text="Prefix2 and Prefix3" Value="Prefix2 and Prefix3" />
</asp:DropDownList>

And here is the relevant JavaScript code inside a .js file:

function showTR() {
    var dropdown = document.getElementById( "<%=lstFilePrefix1.ClientID%>" ); 
    var selectedItemValue = dropdown.options[ dropdown.selectedIndex ].value; 
    var div2 = document.getElementById( "data" ); 

    if( selectedItemValue == 'Prefix2' ) {
        div2.style.dispaly= "block";
    } else {
        div2.style.display = "none"; 
    }
}

Answer №1

It's possible that there is a misunderstanding, as JavaScript does not typically support ASP code. However, it may be feasible to output inside.js as ASP with the correct header. Additionally, there appears to be a typo at div2.style.dispaly.

function showTR() {
    var dropdown = document.getElementById( "#anID" ); // Obtain a reference to the dropdown (select) element
    var selectedItemValue = dropdown.options[ dropdown.selectedIndex ].value; // Retrieve the selected item's value using the dropdown reference
    var div2 = document.getElementById( "data" ); // Access div2

    if( selectedItemValue == 'Prefix2' ) {
        div2.style.display = "block";// Display div2 if the selectedItemValue is 'Action'
    } else {
        div2.style.display = "none"; // Conceal div2 otherwise
    }
}

Answer №2

Employ

div2.style.visibility = "visible";

or utilize

div2.style.visibility = "hidden";

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

Creating multiple select drop-down listboxes in PHPWould you like to learn how to make

Seeking guidance on generating select drop-down lists in PHP. The code I have written only seems to work for the first drop-down, not the second... Here is the code snippet that has been causing me trouble: <?php while($row = my ...

What is the best way to transmit a username and password to an express server using AJAX?

Check out the ajax method below for sending the form to the server: $("#submit").click( function() { $.ajax({ url: '/login', type: "POST", dataType: "html", contentType: 'application/x-www-form-urlencode ...

Is there a way to restrict keyboard input on this date picker?

Can you review the following link and let me know which properties or events I should utilize in this scenario? https://codesandbox.io/s/charming-frost-qrvwe?file=/src/App.js ...

Experiencing significant delays in keyboard response when using an Angular application on mobile browsers such as Safari

Experiencing a frustrating delay in keyboard response when using an Angular app on mobile Safari and Chrome. Surprisingly, there are no such issues when typing on desktop browsers. Any suggestions for troubleshooting this problem? My hunch is that it may ...

Slide in the Toggle Effect to Your Navigation Menu

Seeking help with enhancing my jQuery dropdown menu to include a slide down toggle effect similar to this example: http://jsfiddle.net/LaSsr/188/. Any assistance in applying this slide effect to the following JSfiddle would be greatly appreciated. Thank yo ...

How can we declare React context functions to avoid any potential linting problems?

Working with React Context, I currently have: const DocumentContext = createContext< [DocumentStateType, React.Dispatch<any>, React.Dispatch<any>] >([initVal, () => { }, () => { }]); However, I am receiving a complaint from my ...

Setting the x-api-key header with HttpInterceptor in Angular 4: A guide

I am attempting to use HttpInterceptor to set the header key and value, but I am encountering the following error: Failed to load https://example.com/api/agency: Response to preflight request doesn't pass access control check: No 'Access ...

How can I use jQuery to automatically change the background color of each item to match the color of the next

I have a list with 4 items, each item has a specific background color set in the style attribute. <div class="list"> <div style="background: red"></div> <div style="background: blue"></div> <div style="background: gr ...

ng-bootstrap's accordion imparts unique style to its child icons

https://i.sstatic.net/YzmtN.png Visible star icon indicates lack of focus on the accordion https://i.sstatic.net/aNW31.png Star disappears when accordion gains focus Below is the CSS code I used to position the star to the left of the accordion: .icon ...

Utilizing the Vue feature of dynamic route naming within a component's function

Currently, I am working on creating a dynamic view rendering using Vue and Laravel. However, I am facing difficulty in understanding how to pass the dynamic parameter to the component function. Router.map({ '/cms-admin/:page': { comp ...

How can we identify context menu events triggered by touch actions?

In my application, I have implemented drag-and-drop functionality for elements within an SVG element using d3-drag and d3-zoom. However, on touch-enabled devices such as IE11, Edge, and Firefox, a context menu pops up after a long press, which interferes w ...

React and Redux are throwing an error stating that actions must be simple objects. For any asynchronous actions, it is recommended to utilize

Below is the code for my Component UserHandler import React, { Component } from 'react'; import { connect } from 'react-redux'; import PropTypes from 'prop-types'; import * as actionCreators from '../../store/actions/ac ...

Issue detected in React Rollup: the specific module 'name' is not being exported from the node_modules directory

Currently in the process of creating a library or package from my component. The tech stack includes React, Typescript, and various other dependencies. Encountering an error while using Rollup to build the package: [!] Error: 'DisplayHint' is ...

Retrieve a specific line of code from a snippet of JavaScript

I am looking to extract a specific line of code from a script that I am receiving via ajax... The line in question is new Date(2010, 10 - 1, 31, 23, 59, 59) found within the following snippet: jQuery(function () { jQuery('#dealCountdown').count ...

Using React to apply various filters to an array

Just getting started with react and working with an array of objects named arrayToFilter that I need to filter in multiple ways. Whenever a user changes the filter options, all filters should be applied to the array and the results should be stored in a fi ...

`Three.js and its efficient use of JavaScript for enhancing performance``

Deep within the Object3D code lies: rotateX: function () { var v1 = new THREE.Vector3( 1, 0, 0 ); return function ( angle ) { return this.rotateOnAxis( v1, angle ); }; }(), But what about: rotateX: function (angle) { var v1 = new ...

How to pass parameters while updating parent values in VueJS using emit?

Explaining my dilemma with uploading images: In my form, users can upload images using the following code snippet: <input id="visualisation_upload" @change="onThumbnailChanged" name="visualisation_upload" accept="imag ...

Strange Puppeteer conduct

Currently, I am utilizing Puppeteer to launch a headless web browser and interact with a specific website using JavaScript. The website requires login credentials before proceeding further. Upon login, a popup window appears which closes once the authentic ...

Notifying a Child Component in React When a Props-Using Function Resolves a REST Call

When I submit an item or problem, I trigger a function in the parent component that has been passed down to the child component as props. After sending my information, I want to clear the input fields. The issue is that I am clearing the input fields immed ...

Troubleshoot the issue of the service function not being triggered

Seeking help with my Angular project as I am facing an issue with resolve and $resource. Despite spending a whole day on it, I couldn't find a solution. When using resolve to fetch data with $resource and ui-router, the service method never gets calle ...