capturing the null object in JavaScript

I am having trouble determining if the object is null or not an object. I tried using this line in an if condition to solve it, but it's not working.

However, I keep getting an ERROR message stating "object is null or not an object".

var preWynum = "";
function paint(Wynum)
{
    // alert("Wynum:"+Wynum +", preWynum:"+preWynum);
    if(document.getElementById(preWynum) && document.getElementById(Wynum))
    {
      document.getElementById(preWynum).style.backgroundColor='white';
      document.getElementById(Wynum).style.backgroundColor='yellow';
    }

   preWynum = Wynum; 

}

I'm puzzled as to why it's not running. Any other suggestions?

preWynum and Wynum represent the id of a table row (tr).

I am trying to set the background color of the currently selected row (which has the id of Wynum) to yellow.

Answer №1

The issue arises from passing an object (which doesn't exist) to the getElementById() method instead of a string:

// Make sure 'preWynum' and 'Wynum' are enclosed in quotes
if(document.getElementById('preWynum') && document.getElementById('Wynum'))
{
  document.getElementById('preWynum').style.backgroundColor='white';
  document.getElementById('Wynum').style.backgroundColor='yellow';
}

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

Refrain from showing content beneath a certain element

Is there a way to hide all content that appears after a specific element, such as a particular class of div? The issue I am facing involves using a 1&1 webpage builder with a restrictive layout-template enforced by my boss. I am trying to remove the foote ...

Display the contents of a post within the same page using ReactJS

I am currently in the process of setting up a blog. In my index.js file, I have a left sidebar that displays the links to the articles. My goal is to have the post content show up in the right sidebar of the index.js file when I click on a link, instead of ...

CSS transformation on the go

Can anyone help me? I am trying to create an animation for a hamburger menu when checked and unchecked. I have managed to animate the menu, but I'm struggling with animating the left menu when transforming it to 0. &__menu { transform: transla ...

Tips for managing lag caused by large raw image re-renders in a React application

When trying to change the background position of a user-uploaded background image that is in raw Data URI format using CSS, I noticed that re-rendering becomes slow if the image size exceeds 1mb. This issue does not occur with smaller images. Is there a ...

Turn off PrettyPhoto functionality in the code base

Is there a way to deactivate PrettyPhoto once it has been activated? $(document).ready(function () { $("a[rel^='prettyPhoto']").prettyPhoto(); } $("#disablePrettyphoto").click(function (e) { $("a[rel^='prettyPhoto']").KILLPRET ...

Tips for handling datetime in angular

Currently, I am working with Angular (v5) and facing an issue related to Datetime manipulation. I am trying to retrieve the current time and store it in a variable. After that, I need to subtract a specified number of hours (either 8 hours or just 1 hour) ...

Examining a specific element node to see if it contains a certain string

When using PHP Unit with Selenium Server, my goal is to verify if a specific element in an xpath contains a certain string value, like this: <table> <thead></thead> <tbody> <tr> <th>1</th> < ...

What causes the discrepancy in the output values of the sha1 algorithm when using the packages object-hash and crypto/hashlib

On my frontend, I have implemented a JavaScript function that compares two sha1 hashes generated by the object-hash library. This is used to determine if there are any changes in the input data, triggering a rerun of a processing pipeline. To interact wit ...

Event activates upon exiting hover

I have implemented a functionality where a div is hidden when the user hovers over one of the menu elements. Here is the code snippet that accomplishes this: jQuery("#menu-item-15").hover(function(){ jQuery(".bootom-menu").css("display", "none"); }); How ...

NodeJS: Issue with Route is disrupting the functionality of the Restful API

Struggling to develop an API using Node.js and Express, encountering routing issues with express.Router(). Take a look at my code below: Server.js file contents: // Get necessary packages var express = require('express'); var app = express(); ...

Every time Chrome on Android returns a keyCode of 229

Here is a snippet of code that I am having trouble with: ... @HostListener('keydown', ['$event']) onKeyDown(evt: KeyboardEvent) { console.log('KeyCode : ' + evt.keyCode); console.log('Which : ' + evt.which); ...

The "loose" mode is not resolving the issue with the lack of support for the experimental syntax 'classProperties' that is currently disabled

Error Message: The experimental syntax 'classProperties' is currently not enabled Even after trying the suggested solutions, I still encounter the error during re-building. Click here for more information on the experimental syntax 'classP ...

What methods can I employ within an AngularJS controller to retrieve data from Google App Engine instead of solely relying on a JSON file?

As a newcomer to web development, I have been experimenting with retrieving data from a Google App Engine datastore and selectively displaying it on a webpage using AngularJS. While I have successfully integrated Angular with a JSON file and posted the con ...

Automatically adjusting the height of an iFrame using jQuery

Is there a way to automatically adjust the height of my iframe based on its content? I've successfully achieved this using a jQuery plugin called jquery-iframe-auto-height by house9. Now, I want the homepage within the iframe to fill the entire heigh ...

``Is there a way to display fetched data in a horizontal layout instead of vertical using angularJS?

I'm in need of generating reports using AngularJS and PHP. Desired Output: Spec_ID Bot_Name Color Colorno Screen AN/SN/18 750ML POCO PESCA ROTATION 1 Light Buff Dark Buff Red P ...

Ways to refresh a nested form

I'm currently learning Ruby on Rails and working on developing a web application. The app consists of a classic model with Users who can create Posts. Another model called Online is used to display the posts on a common wall, and it has an associatio ...

Transforming JSON data into comma-delimited values (with thousands separators) using Angular 5 and ES6 syntax

Is there a more elegant method to convert a JSON response into comma-separated numbers for displaying currency purposes? Here is the code I have currently: let data = { "business":{ "trasactionTableData":[ { ...

The 'otherwise' controller in AngularJS does not execute when the resolution in another controller has not been successful

Imagine having routes set up in the configuration: $routeProvider .when('/person/:person_id', { controller: 'person', templateUrl: 'partials/person.html', resolve: { data: ['api', '$route', ...

Experimenting with asynchronous functions in React using Jest

As a newcomer to unit testing, I am working with two files: RestaurantReducer import * as Const from '../constants/Const' const RestaurantReducer = (state = { restaurantList: [] }, action) => { switch (action.type) { case Co ...

Problem saving videos when using the right-click function

Hello, I am currently working on retrieving videos from a database, but I have encountered an issue with the video saving option. When a user right-clicks on the video, they are able to save it in their system's storage. What I would like to achieve ...