Error 0x800a138f - JavaScript runtime: Cannot retrieve the 'style' property of an undefined or null object

I encountered a JavaScript

runtime error: Unable to get property 'style' of undefined or null reference

error while executing the code below.
The issue arises with this line of code:

lblSatisfyQuarterEndDate.style.display = "none";
Removing this line resolves the error. The desired functionality is to hide the textbox and Label when the value is False.

HTML

<div class="tdControl col-xl-6 nowrap">
     <asp:DropDownList ID="ddlSatisfyQuarterEnd_ID" runat="server" onchange="dd_changed()" ClientIDMode="Static" >
        <asp:ListItem  Selected="True" Value="-1">-- Select one --</asp:ListItem>
        <asp:ListItem Value="False">No</asp:ListItem>
        <asp:ListItem Value="True">Yes</asp:ListItem>
     </asp:DropDownList>

     <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ddlSatisfyQuarterEnd_ID" CssClass="ValidatorFailedImage" Display="Dynamic" ToolTip="Is Required" InitialValue ="-1" />
</div>
</div>
<div class="col-xl-4 col-lg-4">

<div class="tdLabel col-xl-6  nowrap">
   <asp:label ID="lblSatisfyQuarterEndDate" runat="server" Text=" Satisfy Quarter End Requirement Date:" />
</div>
<div class="tdControl col-xl-6  nowrap">
</div>

JavaScript

var ddlSatisfyQuarterEnd_ID = document.getElementById("ddlSatisfyQuarterEnd_ID").value;
var ddlSatisfyQuarterEndDate = document.getElementById("ddlSatisfyQuarterEndDate");
var lblSatisfyQuarterEndDate = document.getElementById("lblSatisfyQuarterEndDate");


function dd_changed() {
    var ddlSatisfyQuarterEnd_ID = document.getElementById("ddlSatisfyQuarterEnd_ID").value;
    switch (ddlSatisfyQuarterEnd_ID) {
        case "True":
            ddlSatisfyQuarterEndDate.style.display = "inline";

            break;
        case "False":
            ddlSatisfyQuarterEndDate.style.display = "none";
            lblSatisfyQuarterEndDate.style.display = "none";
            break;
        default:
            ddlSatisfyQuarterEndDate.style.display = "none";

Answer №1

It is important to avoid blindly trusting ID's in .aspx markup. Instead, carefully examine the generated HTML code in your browser.
One way to improve this situation is by binding ID's on the server side. Take a look at this example:

var ddlSatisfyQuarterEnd_ID = document.getElementById('<%#ddlSatisfyQuarterEnd_ID.ClientID%>').value;
var ddlSatisfyQuarterEndDate = document.getElementById('<%#ddlSatisfyQuarterEndDate.ClientID%>');
var lblSatisfyQuarterEndDate = document.getElementById('<%#lblSatisfyQuarterEndDate.ClientID%>');

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

Learning how to dynamically update a value in Angular based on user input

My goal is to dynamically change the output value based on user input using Angular. I have successfully implemented the functionality to increment the value, but unfortunately, when the input changes, the outputed value remains static. Below is my curren ...

Tips on preventing right-click actions in jqGrid

While utilizing onSelectRow in a jqGrid, I have noticed that it functions properly when clicking with the left mouse button. However, when right-clicking, it still triggers the function. My aim is for the right-click to perform its usual action (such as di ...

Here's a step-by-step guide on how to use jQuery to open a new page with the same image displayed when clicked

Seeking guidance as a newcomer to javascript, I'm struggling to find the best way to explain this dilemma. Any advice or assistance would be greatly appreciated. Here is the HTML code in question: <div class="col-md-4"> <a href="st ...

The unexpected behavior in React's reconciliation process: What is causing the state to remain unchanged?

While exploring the React documentation, I came across an interesting example of resetting state: here To better understand it, I created different sandboxes to experiment with. However, I am struggling to reconcile what I observe in each of them. Each s ...

What is the proper method for utilizing colspan within the footerData of a jqGrid?

Are you looking to customize the footer of your jqgrid as shown in the example below? I am trying to set up a custom footer for my jqgrid similar to the one displayed above. I have already enabled the footerrow:true option and used $self.jqGrid("footerDat ...

Can you help me troubleshoot an issue I am facing with the expand table in Angular 9 and JS? I am getting an

Here you can find the code demonstration and behavior: No extensive explanation is necessary. Check out the StackBlitz demo by clicking on the first row to view its details. Then, click on the second row to see how the details from the first row are repl ...

Function executed many times during click action

I have developed a web application that allows users to input any keyword or statement and receive twenty results from Wikipedia using the Wikipedia API. The AJAX functionality is working perfectly. The app should dynamically create a DIV to display each r ...

The image fails to display correctly

As I work on creating a basic webpage in HTML and JavaScript, my goal is to validate certain parameters (such as width, height) of an image that users upload via a form. In JavaScript, I extract the file from the form and attempt to display it as an image ...

Is there a Node template engine similar to EJS that seamlessly integrates with HTML templates?

Is there a template engine for NodeJS similar to EJS that doesn't alter the original HTML template structure with its use of parentheses? In EJS, one might utilize the following code snippet to embed specific data into the HTML template: <script& ...

Retrieve information in JSON format using AngularJS by fetching data from SQL through PHP

My objective is to retrieve data from a mySql database using PHP, convert it to JSON, and then display it through AngularJS. Although I have successfully completed the steps leading up to this point, I am facing issues with the final step. Below are the de ...

Is it achievable to send information back to a Servlet from JavaScript?

I am currently working on setting up a Servlet that utilizes JDBC for database interactions. My goal is to allow users to log in through a login form and then view the list of available databases in MySQL. I am implementing this functionality dynamically ...

Ways to safeguard your Asp.net core 2.1 and Vue.js single page application using Identity Server 4

Hello, I've encountered a small dilemma regarding architecture. I have two separate applications and one Identity Server4 Application. One of the applications is an AspNet core 2.1 API solution running on a different domain and server, as well as the ...

Substitute a piece of text within an element while also preserving the formatting of a new line

My goal is to modify the text within the div tag "a/b" to "b/c" while preserving the line break that I have inserted in the HTML using the break tag. Currently, the text is being replaced, but I need the second line to appear below the first line. $(".t ...

AngularJS 1.x directive experiencing unresponsive controller

Currently, I am attempting to organize my menu using a directive. Here is the structure I have set up: function Menu(){ self = this; self.tab = ''; self.selectedTab = function(tab){ self.tab=tab; console.log('updating tab&a ...

Completed Animation with Callback in AngularJS CSS

Currently working with AngularJS and hoping to receive a notification upon completion of an animation. I am aware that this can be achieved with javascript animations such as myApp.animation(...), but I'm intrigued if there is an alternative method wi ...

Incorporate various style components to enhance the appearance of an item in ExtJS

I'm attempting to add ellipsis to an item created by ExtJS. My goal is to only modify this item without adding any additional CSS files. After researching, I discovered there is a "style" parameter that accepts CSS styles and attempted the following: ...

jQuery struggles to locate the active class within the Bootstrap slider

Want to make some changes to the Bootstrap slider? Here is the HTML Code and jQuery: <div id="carousel-slider2" class="carousel slide bs-docs-carousel-example"> <ol class="carousel-indicators"> & ...

Retrieve the text content of a datalist option by accessing the label with jQuery

Utilizing data from a Json, I am populating a data-list in html. The options are added to the data-list with both value and label text. Upon clicking an option, I aim to insert both the value and text into a form text field. While accessing the option&apo ...

serving JavaScript and various other files to HTML through a Node.js HTTP server

My web server setup is quite basic: var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/html' }); fs.readFile('./index.ht ...

NextJS was throwing a warning at me, while Firebase hosting was giving me an error regarding the absence of unique keys for children in lists, even though I

I've been troubleshooting this warning for quite some time, but I can't seem to resolve it. The warning reads as follows: Warning: Each child in a list should have a unique "key" prop. Check the top-level render call using <ul>. ...