Do you actually have an element in the DOM with that id?
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
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>`);
}
}
Please or to participate in this conversation.
