Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

getElementById is not working.

I want to get the body of the products table.

success: function (response) {
    $('#productFilters').empty();
    for(i = 0; i < (response.data).length; i++) {
        let product = response.data[i];
        let body = product.body;
        document.getElementById('text-body').innerText = body.substring(0, 100);
        $('#productFilters').append(`<div class="col-lg-4 mb-3">
        <div class="card">
            <img src="/storage/${product.gallery.image}" class="card-img-top" alt="${product.title}">
            <div class="card-body">
                <h5 class="card-title">${product.title}</h5>
                <aside class="card-text" id="text-body"></aside>
                <a href="${product.path}" class="btn btn-primary">ئخقث هدبخ</a>
            </div>
        </div>
    </div>`);
    }
},

I see this error

filters

0 likes
30 replies
tykus's avatar

Do you actually have an element in the DOM with that id?

aurelianspodarec's avatar

This is going to work my friend!

success: function (response) {
    $.each(response, function(index, element) {
        console.log(element);
    });
}

It's magic, you know.

2 likes
kokoshneta's avatar
Level 27

It‘s a little hard to tell without actually being able to see the HTML output, but if I were to guess, I’d say it’s because you’re trying to set the inner HTML of your <aside id="text-body"> before you actually add it to the DOM.

Additionally, you’re looping through the response data (which means there may be more than one) and then you’re appending an element with an id attribute for each iteration of the loop. That won’t work, because an element’s ID must be unique on the page, so you can’t just add three elements that all end up being <aside id="text-body"> – that’s not valid HTML.

If you make text-body a class instead of an ID and then just set its inner HTML when you append the whole card instead of trying to do it before the element exists, it should work:

success: function (response) {
    $('#productFilters').empty();

    for(i = 0; i < (response.data).length; i++) {
        let product = response.data[i];
        let body = product.body;

        $('#productFilters').append(`<div class="col-lg-4 mb-3">
        <div class="card">
            <img src="/storage/${product.gallery.image}" class="card-img-top" alt="${product.title}">
            <div class="card-body">
                <h5 class="card-title">${product.title}</h5>
                <aside class="card-text text-body">${body.substring(0, 100)}</aside>
                <a href="${product.path}" class="btn btn-primary">ئخقث هدبخ</a>
            </div>
        </div>
    </div>`);
    }
}
2 likes
aurelianspodarec's avatar

@MichalOravec What do they gain from copying other people questions and mold them into one and creating posts like this? I don't get it. Its just spam really. People take time to help out, but in reality he never needed that help as its just fugazi.

aurelianspodarec's avatar

@MichalOravec

You pointed out that he copies and pastes other people questions, and I checked those threads, and he does - isn't that spam? He doesn't need this help does he

shahr's avatar
Level 10

@aurelianspodarec

I need help, I have to write something. That they understand what I want and what I do not want.

1 like
shahr's avatar
Level 10

@Snapey You are the fool who does not answer my question all the time. 😄 🤣 😂

1 like
Snapey's avatar

@boro because you do not like me. Any luck with the office furniture job yet?

Boro = Item = loving = qhom = oxbir

shahr's avatar
Level 10

@Snapey If I change my username, will you answer my question? And you help me?

shahr's avatar
Level 10

@Snapey Now I have 2 very, very important questions. Can you help me if I change my name to oxbir?

Snapey's avatar

@boro no, in that one you word for word copied someone else's question from four years ago

why don't you just read their answers

shahr's avatar
Level 10

@Snapey

I do not know what to write for you to understand.

I copied it from someone else Because I liked this question, I copied.

shahr's avatar
Level 10

@Snapey Answer my question, To change the username forever.

Please or to participate in this conversation.