The NodeList returned by querySelectorAll is not defined

One issue I encountered in this code snippet is that the 'questions' array is empty. I expected the 'ol li' selector to target all list items within ordered lists. Another problem I faced was the inability to assign an id to the array, as seen in the first line of code within the for loop.

function setUp()
{
    var questions = document.querySelectorAll('ol li');
    /*var questions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];*/
    for (var x = 0; x < questions.length; x++)
    {
        questions[x].id = x + 'phrase';
        onmousedown(showEnglish(questions));
        onmouseup(showFrench(questions));
        onmouseover.style.cursor = 'pointer';
    }
}

function showEnglish()
{
    var phraseNumber = parseInt(parent.questions.id);
    document.getElementsById(phraseNumber).innerHTML = english[phraseNumber];
    english[phraseNumber].style.fontStyle = 'italic';
    english[phraseNumber].style.color = 'rgb(191,22,31)';
}

Answer №1

When using document.querySelectorAll('ol li'), you will be able to target all the list items within an ordered list. It is crucial to consider the timing of when you call the setUp function. If this function is called before the elements are actually rendered on the DOM, there is a risk that the resulting array may be empty.

I'm a bit confused about the second part of your inquiry. You mentioned that you are working with the following array:

 questions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

and attempting to add an ID to it?

If this is the case, it will lead to an error. This is because when iterating through the questions array, each element is treated as a string. Therefore, trying to assign a value to questions[x].id will result in an error since questions[x] is a string and not an object.

I hope this clarifies things for you!

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

Converting information from an Excel spreadsheet into a JSON format

Looking for a way to extract values from an excel spreadsheet using Python and send them to a webpage for processing with javascript. I'm considering creating a dictionary object and returning it to javascript in JSON format. The values extracted fro ...

Retrieving data from a textbox using JavaScript within an Excel VBA setting

I'm encountering an issue with a Macro in Excel where I need to retrieve the value of an "input type="text"" on a JavaScript webpage. However, the value is not specified in the code (value=""), and it remains unchanged even when the webpage displays t ...

Ways to display success message following JavaScript validation

I've been working on creating a form that displays a success message after JavaScript validation checks are successful. To show the success message, I'm utilizing Bootstrap alert. I split my code into two files - one for common validation functio ...

Angular5+ Error: Unable to retrieve summary for RouterOutlet directive due to illegal state

When attempting to build my Angular App using ng build --prod --aot, I consistently encounter the following error: ERROR in : Illegal state: Could not load the summary for directive RouterOutlet in C:/Path-To-Project/node_modules/@angular/Router/router.d. ...

Does the arrangement of props matter in JSX?

Assuming the o object has a key/value pair of: foo: 'bar', can I expect these results to hold true?: // foo will be 'bar' <MyComponent foo='should not override' {...o} /> // foo will be 'overridden' ...

Prevent selection based on function in Angular

I'm attempting to prevent certain options from being selected based on a specific method. For instance, let's say I have four options: A B C D In my method (let's use "x" as an example): if(name == A) { disable the selection for option A. ...

Toggle the row to expand or collapse upon clicking it

After extensive research, I have been unable to get the row expansion and collapse feature to work in my script. Here is my current script: <thead> <tr> <th width="5" class="cb"><input type="checkbox" id="cbcall" /& ...

Using AngularJS Typeahead with restrictions on $http requests

I have been attempting to restrict the number of results displayed by Angular Bootstrap Typeahead during Async calls, but unfortunately, it does not seem to be functioning as expected. <input type="text" ng-model="asyncSelected" placeholder="Locations ...

Implementing CORS in ReactJs: A Step-by-Step Guide

When working with React.js, I often use this fetchData config: fetchData = () => { fetch("http://localhost:8000/batch_predict", { method: "POST", headers: { 'Accept': 'application/json, text/plain, */*&apo ...

Issue with JSONP request in jQuery/AJAX

I am currently attempting to make a request to a different site, which requires authentication through a cookie. Oddly enough, when I try making the call like this : $.getJSON(url + '?', function(data){ alert(data); }); The HTTP headers do ...

Leveraging PrimeFaces and the p:ajax component, trigger Ajax within an inputText field only when keystrokes lead to changes in the field

I am currently utilizing PrimeFaces and have a p:inputText field that requires updating certain components on the view based on the most recent keystroke within that p:inputText. Below is the code snippet: <p:inputText value="#{customerLController.surn ...

Steps to activate a particular Bootstrap tab upon clicking a hyperlink

Trying to implement a Bootstrap tab panel in the following manner: <ul class="nav nav-tabs" role="tablist"> <li class="nav-item"> <a class="nav-link active" data-toggle="tab" href ...

Obtaining the value of a JavaScript confirm popup in C#.NET

I am trying to implement a javascript confirm popup that returns a value from the code behind. When the user selects the ok button on the confirm popup, certain code will execute, and if they select cancel, different code will run. Is there a way to retri ...

How can I modify an array in Couchbase by using an N1QL query?

{ "name":"sara", "emailId":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abcde8e2ea9627225c4b9a3afa7bbcc808cb554a1adaf">[email protected]</a>", "subjects" : [{ "name":"Math", "tutor":"alice", "classes" ...

It appears that the crackling noise is being generated by AudioContext.decodeAudioData

I am currently in the process of developing an electron app that enables users to cut and rearrange multiple audio samples while seamlessly playing them back. The combined duration of these samples can exceed an hour, making it impossible to decode and sto ...

Accessing Google Maps using latitude and longitude coordinates from a Json file

I am having trouble displaying markers on the map when trying to open it with latitude and longitude from a Json file. The map is opening, but the markers are not showing up. Currently, I am fetching a Json file using PHP, creating a variable array, and t ...

"Learn the process of extracting information from a database and exporting it to a CSV file with Angular 2

Currently, I am facing an issue where I need to retrieve data from a database and export it to a CSV file. While I am able to fetch all the data successfully, I am encountering difficulty when trying to fetch data that is in object format as it shows up as ...

Material UI - sending an icon as a property

I need help with inserting a material-ui icon into my component using props. Can you please advise on what I might be doing wrong? I am struggling with passing the icon down in JSX, and here is the attempt that is not working: This code snippet shows me t ...

Different ways to maintain the original syntax highlighting colors in JavaScript console

Upon closer inspection near the green arrows, you can see that the default console.log function colorizes values based on their type, distinguishing between string and number. https://i.sstatic.net/MtO8l.png In contrast, highlighted near the red arrows i ...

Retrieve the access ID from the conn.query result

When I run a SQL query, I need to extract the primary key (id) of the event returned so I can use it in another SQL query. However, attempting to access it using result.insertId returns null for the event object. Even logging result.insertId only outputs ...