Show elements on a webpage using HTML and JavaScript

How can I showcase film1, film2, and film3 in my HTML code? I attempted:

peliHtml.innerHTML = `${this.Film.title};
    

However, I keep encountering a console error. How should I go about displaying this data in the html?

HTML

<p id="films"></p>
    

JAVSCRIPT


    class Film {
        title = '';
        director = '';
        genre = '';
        date = '';
        getDates(){
            return `marca: ${this.title} - director: ${this.director}, genre: ${this.genre}, date: ${this.date}`;
        };

        constructor(title, director, genre, fechaEstreno){
            this.title = title;
            this.director = director;
            this.genre = genre;
            this.date = date;
        }

    }

    let film1 = new Film('El hobbit', 'peter jackson', 'aventura', '23-02-2002');
    let film2 = new Film('El hobbit 2', 'peter jackson', 'aventura', '23-02-2010');
    let film3 = new Film('El hobbit 3', 'peter jackson', 'aventura', '23-02-2020');

    const peliHtml = document.getElementById('films');
    peliHtml.innerHTML = `${this.Film.title}`;

    

Answer №1

this specifically pertains to an instance of a Film object within the Film class. It is generally recommended not to use this outside of a class context.

If you wish to showcase all the film titles, it would be more effective to store all the Film objects in an array, utilize .map() to extract the .title attribute for each one, and then combine them with commas:

class Film {
  title = '';
  director = '';
  genre = '';
  date = '';
  getDates() {
    return `Title: ${this.title} - Director: ${this.director}, Genre: ${this.genre}, Date: ${this.date}`;
  };

  constructor(title, director, genre, date) {
    this.title = title;
    this.director = director;
    this.genre = genre;
    this.date = date;
  }
}

let film1 = new Film('The Hobbit', 'Peter Jackson', 'Adventure', '23-02-2002');
let film2 = new Film('The Hobbit 2', 'Peter Jackson', 'Adventure', '23-02-2010');
let film3 = new Film('The Hobbit 3', 'Peter Jackson', 'Adventure', '23-02-2020');
const films = [film1, film2, film3];

const filmsHtml = document.getElementById('films');
filmsHtml.innerHTML = films.map(film => film.title).join(', ');
<p id="films"></p>

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

Opening external stylesheets in a separate window

Objective: Create a new window, add elements to it, and then apply CSS styles to the elements in the new window using an external style sheet. Issue: While I am able to add elements to the new window and link it to an external style sheet in the head sect ...

JavaScript Deviance

I am facing an issue with my JS code while trying to send form data to a .php file via AJAX. The problem occurs when the input fields are filled - for some reason, my client-side page refreshes and the php file does not get executed. However, everything wo ...

Each page in NextJS has a nearly identical JavaScript bundle size

After using NextJS for a considerable amount of time, I finally decided to take a closer look at the build folder and the console output when the build process is successful. To my surprise, I noticed something peculiar during one of these inspections. In ...

My mocks for Jest's readFile and writeFile functions are not being loaded

Just wondering, when using jest.mock(), can I only mock the entire module or can I also mock exported functions and constants? In my app.js file, I am using const fileSystem = require('fs'). I am trying to test an method in the app that reads a f ...

display saved data from ajax request

I've been working on saving data and files using ajax. Following a tutorial (link), I managed to successfully save the data in the database and the image files in the designated folder. However, I'm facing an issue where the success or error mess ...

The Joomla jCE popup feature is not functioning properly when used with AJAX content

Currently, I am using Joomla for my project. One of the features I have implemented is Ajax to display a specific section on a page. Within this Ajax-loaded section, there is a JCE popup link included in the code: <a href="some link" class="jcepopup no ...

Unable to retrieve chosen rows using Angular UI Grid

My scenario involves managing two grids with selectable rows. I aim to retrieve the data from the selected rows and store it in a separate object to send it to a backend service. Here is the relevant code snippet: angular.module('gridPanel',[&ap ...

Does LABJS include a feature for executing a callback function in the event of a timeout during loading?

When using LabJS to asynchronously load scripts with a chain of dependencies, if one of the scripts breaks (due to download failure or connection timeout), it seems that the remaining scripts in the chain will not be executed. Is there a way to define a ...

The VLC WebPlugin puts a halt on websocket activity during playback

I currently have a basic HTML/JS application that includes an embedded VLC WebPlugin and several methods for play/pause functionality. This application is being managed by another JavaScript file through a straightforward server/websocket C# setup. The con ...

Retrieve the URL with a GET request and remove a specific object

Currently, I am working on developing a CRUD (Create, Read, Update, Delete) App using Express and LowDB. So far, I have successfully implemented the create and read functions, but I am facing issues with the delete function. This is an example of what th ...

Utilizing accurate server URLs (Codeigniter) for local JavaScript execution

Help Needed: Issue with Correct Path Names in Local JavaScript with CodeIgniter Backend I am facing difficulties in using the correct path names in local JavaScript while working with a backend in CodeIgniter. -user_data -application -system -assets In ...

Unable to successfully add element to array using UIKit modal in vuejs

On my webpage, I have a table that showcases an array of "currency" objects: <tbody> <tr v-for="currency in currencies" v-bind:key="currency.Name"> <td class="uk-width-medium">{{currency.Enabled}}</ ...

A method for automatically collapsing one div item when another is open

I have tried various methods but none seem to work. I am attempting to open only one div at a time, please click on the link above to see for yourself. Please provide any alternate solutions or suggestions. JsFiddle: http://jsfiddle.net/gm2k3ewp/ < ...

My type is slipping away with Typescript and text conversion to lowercase

Here is a simplified version of the issue I'm facing: const demo = { aaa: 'aaa', bbb: 'bbb', } const input = 'AAA' console.log(demo[input.toLowerCase()]) Playground While plain JS works fine by converting &apo ...

The hamburger icon seems to be frozen and unresponsive

I'm struggling to get my hamburger icon to reveal the navigation bar when clicked. The icon itself functions properly and adapts to different screen sizes, but the overlay remains stationary. Check out my code on JSFiddle! <html> <head> ...

Tips for setting variable values in Angular 7

I'm encountering an issue with assigning values to variables in my code. Can anyone provide assistance in finding a solution? Here is the snippet of my code: app.component.ts: public power:any; public ice:any; public cake:any; changeValue(prop, ...

Prevent tab key focus in jQuery UI autocomplete

Sharing my own answer here: Here is the HTML code I am working with: <select id="genre-select"> <option value="-1">Please select</option> <option value="1">One</option> <option value="2">Two ...

Utilizing onClick with material-ui button - functioning flawlessly for a single interaction

I have been attempting to encapsulate a Material-UI button within another component. Everything seems to be working well, except for when I try to handle the onClick event - it appears to only work once. Here is an example that demonstrates the issue: ht ...

The NODE.JS application becomes unresponsive when attempting to retrieve 2.5 million records from an API

I'm facing an issue where my app is retrieving millions of rows from a database and API, causing it to get stuck after calling the getData() function. let closedOrdersStartDate; preparedOrdersPromise = tickApiConnector.obtainT ...

Utilize pivot to manage user roles and permissions in ExpressJS application using Mongoose

My user schema is structured as shown below const userSchema = mongoose.Schema({ username: { type: String, required: true, }, first_name: { type: String, required: true, }, last_name: { type: Stri ...