0

when i click on button

<button onclick="getOneFilm(${this.id})" class="btn position-absolute hre btn-success col-12">more details</button>

i get error

Uncaught ReferenceError: tt4853102 is not defined at HTMLButtonElement.onclick

Here is full js code

let data
let films
const app = document.querySelector('.application')
const click = document.querySelector('.click')
const input = document.querySelector('.searchFilm')
const clear = document.querySelector('.times')

clear.onclick = () => {
    input.value = ''
}

click.addEventListener('click', () => {
    app.innerHTML = ''
    getData(input.value)
        .then(res => {
            data = (JSON.parse(res)).Search
            data.length = 2
            films = data.map(item => new Films(item.Poster, item.Title, item.Type, item.Year, item.imdbID))
            films.forEach(film => film.renderIn(app))
        })
})
function getData(filmName) {
    return new Promise(resolve => {
        const xhr = new XMLHttpRequest
        xhr.open('GET', `http://www.omdbapi.com/?s=${filmName}&page=2&apikey=eeb56d4b`)
        xhr.send()

        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4 && xhr.status == 200) {
                resolve(xhr.response)
            }
        }
    })
}

function getOneFilm(id) {
    return new Promise(resolve => {
        const xhr = new XMLHttpRequest
        xhr.open('GET', `http://www.omdbapi.com/?i=${id}&apikey=eeb56d4b`)
        xhr.send()

        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4 && xhr.status == 200) {
                resolve(xhr.response)
            }
        }
    })
}

function wrapper(id) {
    getOneFilm(id).then(res => {
        console.log(res)
    })
}

class Films {
    constructor(poster, title, type, year, id) {
        this.poster = poster
        this.title = title
        this.type = type
        this.year = year
        this.id = id
    }

    renderIn(elem) {
        console.log(this.id)
        elem.innerHTML +=
            `<div class="card position-relative  mr-2 mb-2" style="width: 16rem;height:34rem">
                    <img class="card-img-top" height='350px' src="${this.poster}" alt="Card image cap">
                    <div class="">
                        <h5 class="card-title text-center p-2">${this.title}</h5>
                        <p class="card-text text-center">${this.type}</p>
                        <p class="card-text text-center">${this.year}</p>
                        <button onclick="getOneFilm(${this.id})" class="btn position-absolute hre btn-success col-12">more details</button>
                    </div>
                </div>`
    }

}
  • 1
    You need to add quotes around the `${this.id}` like `getOneFilm('${this.id}')`. Otherwise it will become `getOneFilm(tt4853102)` where `tt4853102` is an identifier and not a string. – Ivar Nov 27 '22 at 10:59
  • Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. Use [event delegation](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of adding several event listeners — it’s more maintainable and applies to dynamically added elements. – Sebastian Simon Nov 27 '22 at 11:25
  • See [the tag info](/tags/event-delegation/info) and [this Q&A](/a/55452921/4642212). Remove all `onclick` attributes and use an attribute such as `data-film-id="${this.id}"`. Use the [event argument](//developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener#The_event_listener_callback): `app.addEventListener("click", ({`[`target`](//developer.mozilla.org/en/docs/Web/API/Event/target)`}) => { const getFilmButton = target.closest("[data-film-id]"); if(getFilmButton){ getOneFilm(getFilmButton.dataset.filmId) } });`. Also, consider using ` – Sebastian Simon Nov 27 '22 at 11:30

0 Answers0