How to add a className to an object in array in js?

Joined
Dec 7, 2021
Messages
1
Reaction score
0
I have an array of objects. I want to add a class for all the objects in the array. I tried to use classNameand classList but it isn't worging. It shows me: Property 'classList' does not exist on type '{ question: string; answer1: string; answer2: string; answer3: string; answer4: string; }'.ts(2339)

JavaScript:
const questions = [];
questions[0] = {
    question: 'מהי מסילת הרכבת הארוכה בעולם?',
    answer1: 'הכחולה',
    answer2: 'הטרנס-סיבירית',
    answer3: 'סובבת הקרחון',
    answer4: 'האוריינט אקספרס'
};
questions[1] = {
    question: 'מגלודון הוא מין פרה היסטורי של...',
    answer1: 'דינוזאור',
    answer2: 'כריש',
    answer3: 'אדם קדמון',
    answer4: 'כלב'
};
questions[2] = {
    question: 'מהי פיתקיונופוביה?',
    answer1: 'פחד מתיקים',
    answer2: 'פחד מפחדים',
    answer3: 'פחד ממדבקות',
    answer4: 'פחד מפתקים'
};

for(let i = 0; i < questions.length; i++) {
    questions[i].classList.add('hide');
}

How can I add this class?
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
classList is a property of HTML/DOM elements, not of bare JavaScript objects. I assume you're creating HTML elements out of the questions and those elements should get that class. Below is an example of creating paragraph tags for each question, with the text of the paragraph being the question itself. If you use the DOM inspector on your browser, you can see that they do have the 'hide' class. You can use the questionElements array to reference those tags later in your script.

HTML:
<html>
    <body>
        <script>
            const questions = [];
            questions[0] = {
                question: 'מהי מסילת הרכבת הארוכה בעולם?',
                answer1: 'הכחולה',
                answer2: 'הטרנס-סיבירית',
                answer3: 'סובבת הקרחון',
                answer4: 'האוריינט אקספרס'
            };
            questions[1] = {
                question: 'מגלודון הוא מין פרה היסטורי של...',
                answer1: 'דינוזאור',
                answer2: 'כריש',
                answer3: 'אדם קדמון',
                answer4: 'כלב'
            };
            questions[2] = {
                question: 'מהי פיתקיונופוביה?',
                answer1: 'פחד מתיקים',
                answer2: 'פחד מפחדים',
                answer3: 'פחד ממדבקות',
                answer4: 'פחד מפתקים'
            };
            
            var questionElements = [];

            for(let i = 0; i < questions.length; i++) {
                var el = document.createElement('p');
                el.innerText = questions[i]['question'];
                el.classList.add('hide');
                document.body.appendChild(el);
                questionElements.push(el);
            }
        </script>
    </body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,676
Latest member
ManuelaHol

Latest Threads

Top