Struggling to retrieve data from the DOM that was passed as a string. Looking for guidance on using an external function that I have included in the code

Need help with updating the appearance of a card when clicking on the "Important" link? The goal is to change the background color of the corresponding card and save the changes in local storage. However, encountering issues when accessing the DOM file passed as a string. For example, unable to access

document.getElementsByClassName("noteCard")
within the function markNotes(index). Despite this,
console.log("Color is not applied")
runs successfully. When attempting to change the body background color using
document.body.style.backgroundColor = "lightblue";
, it works as expected. The main challenge lies in changing the background color of the cards with the class "noteCard" specifically. Any insights or solutions would be greatly appreciated.

Below you'll find the HTML code:

...

And here's the JavaScript code:

...

Answer №1

An important detail you overlooked was the use of index when fetching an element within the function markNotes:

function markNotes(index) {
   let notes = localStorage.getItem("notes");
   if (notes != null) {
    notesObj = JSON.parse(notes);
   } else {
    notesObj = [];
   }
  let noteCard = document.getElementsByClassName("noteCard")[index];
    noteCard.style.color = "lightblue";
    console.log("Color is applied")
   localStorage.setItem("notes", JSON.stringify(notesObj));
   //showNotes(); you don't need to add this again
}

For a fully functional code example, check out this fiddle: link

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 an array using the variables in a class

I am seeking a way to create an array in TypeScript that contains the values of existing class variables. For example: export class example{ static readonly a = 'aval'; static readonly b = 'bval'; static readonly c = 'cval&a ...

Compiling Vue components upon insertion into the DOM

Is there a way to dynamically insert a compiled element into the DOM, without having a pre-defined location as instructed in the documentation? var res = Vue.compile('<div><span>{{ msg }}</span></div>') new Vue({ data: ...

Provide MongoDB ID for SQL Server across several entries

I am facing an issue with my data migrator tool that is moving data from SQL Server to Mongo. The problem occurs when trying to update the SQL table by inserting the generated ID from Mongo into it. An error message "RequestError: Requests can only be made ...

Is there a way to create a navigation menu that highlights as we scroll down the page?

Check out this example of what I am looking for. https://i.stack.imgur.com/fdzvZ.png If you scroll, you will notice that the left menu highlights. Sub-menus will extend when the corresponding menu is highlighted and collapse when other menus are highlig ...

Accessing data from an API using Node.js

I'm currently working with Node.js and trying to retrieve the city name from an API. However, I keep encountering an error message that reads "Cannot read property city_name of undefined." The issue seems to be arising from this specific line of code ...

Real-time Calculation and Validation of Input Field Totals Using JQuery with Modulus % Rules

Hello, I need assistance with validating my input fields to accept values that are divisible by certain numbers. The criteria are as follows: inputValue % 10 == 0; inputValue % 20 == 0; inputValue % 50 == 0; inputValue % 100 == 0; inputValue % 200 == 0; ...

Issues with handling JSON data using SwiftyJSON

I have been attempting to utilize SwiftyJSON to parse the data provided below: { "state": { "on": true, "bri": 100, "alert": "none", "mode": "homeautomation", "reachable": true }, "swupdate": { "state": "noupdates", "lastinstal ...

Determining if a user is already logged in from a different device using express-session

After a user logs in, I assign the username to their session with the code: req.session.username = "...."; This identifies the session with a username, but now I need to figure out how to detect if this same username is already logged in from another dev ...

Deciphering cURL data within a Ruby on Rails application

This is the first time I am exploring the world of curl within my Rails 4 application to integrate Plaid with Stripe. The process involves successfully exchanging the public token for the Stripe bank account token. Discover more about Stripe with Plaid AC ...

How to Validate a JSON Key's Value in PHP

I'm dealing with this PHP function: <?php function showTable(){ $url = "http://10.0.0.1/lib/api/desk/branch/"; $params = array ("action" => "list","company_key" => "1"); $result=requestURL($url,$params); $json_a=json_deco ...

How can I create a selenium web driver test on Ubuntu 20.04?

Currently, I am working on setting up a test using selenium-webdriver in my project running on Ubuntu 20.04 I successfully installed it using yarn add selenium-webdriver. However, I am facing an issue with safaridriver which seems to be missing. Whenever ...

Error handling in Spring MVC's @ResponseBody annotation

I am encountering an issue with passing an entity GroupStudent object from my Spring Controller to a JSP page using an ajax function. When attempting to pass the GroupStudent object using @ResponseBody, I consistently receive an error in the browser: Error ...

Show a popover within a parent div that has limited visible space due to its hidden overflow

Struggling with AngularJS, I can't seem to find a simple solution for this problem. In my code, there's a div element with the overflow: hidden property set due to an internal scrollbar. Inside this div, there's a dropdown menu that is trigg ...

Using PHP variables in JavaScript to access getElementById

I have multiple forms displayed on a single PHP page. They all follow a similar structure: <form id="test_form_1" action="test_submit.php" method="post" name="test_form"> <label>This is Question #1:</label> <p> &l ...

Sorting price and date with Vue in Laravel - A beginner's guide

Attempting to sort by price and by date. See the code below. I decided not to use the controller for this sorting. Is it feasible to implement it here? It's somewhat functional, but there are some issues. When selecting price, it doesn't seem to ...

Using Multiline Strings for Arguments

Is there a way to successfully pass multi-line strings containing spaces and tabs as a parameter to an express server? Below is the Express.js code snippet which accepts the parameter: app.get('/:prompt', async (req, res) => { ...

Encountering difficulties retrieving JSON response from Express in a production environment

When in Development mode, react and express connect flawlessly, allowing for successful data retrieval from the backend. However, in Production mode, although a status code of 200 is returned, the JSON response from express cannot be obtained. The specifi ...

React Router will not remount the component when using this.context.router.push

We have implemented a click handler that utilizes react router 2.0 to update the URL with this.context.router.push(). Here is the code snippet: selectRelatedJob(slug) { JobActionCreators.fetchJobPage(slug); JobsActionCreators.getRelatedJobs({'sl& ...

Adjust the Scope in Angular-Charts.js Post-Rendering

I am currently facing a challenge with displaying multiple charts using the angular-charts.js framework. The issue is that I require all the charts to have the same scale, but each chart currently has its own scale based on the displayed data. Unfortunatel ...

Modifying npm packages within a web application

I have a web application where I recently installed an npm package. However, I've realized that I need to customize it by adding some code. My attempt to modify the package directly in node_modules hasn't resulted in any visible changes. Is there ...