I called an api from a website to make a list of items in my own website.
I do that withforeach
. The list gets imported correctly, but I have a little problem.
Each item in the list has an description withdisplay: none;
and each item also has a button. If I click that button I want to make thedisplay: inline;
I tried a lot of things, but every time I click a button only the first item in the foreach list changes.
I am not really good at explaining, but hopefully someone can help me.
<?php
foreach ($items as $item):
echo "<script>
function hide_post(){
hideshow = document.querySelector('#showMoreLessId');
hideshowtext = document.querySelector('#showMoreLess');
if (hideshow.style.display == 'inline'){
hideshow.style.display = 'none';
hideshowtext.innerHTML = 'Toon meer';
}else{
hideshow.style.display = 'inline';
hideshowtext.innerHTML = 'Toon minder';
}
}
</script>";
?>
<div class="item" id="item">
<h4>Versie: <?= $item->name; ?></h4>
<div id="showMoreLessId">
<p>Issues in versie: <?= $res->issuesFixedCount; ?> </p>
<p>Afgeronde issues: <?= $roundedIssue;?> </p>
<p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
</div>
<small><?= $item->releaseDate; ?></small>
<a onclick="hide_post()" id="showMoreLess">Toon meer</a>
<a>|</a>
<a href="<?='version/?version=' . $item->name; ?>">Meer info</a>
</div>
<?php
endforeach;?>
</div>
</div>
Your issue is because you're repeating the sameid
on multiple elements in the DOM as you loop to create the HTML.
To fix this issue applyclass
attributes to the repeated content instead, and then usedocument.querySelectorAll()
to retrieve them. Note that you will need to loop through the returned collection as well.
In addition you will need to use DOM traversal to find the elements related to the clickeda
element.
In addition, your JS function should only be declared once, outside of your PHP loop.
Here's a working example with the above changes applied. Note that I also changed the class names to better describe the purpose of the elements.
// JS logic only defined once, separate from your PHP code...
document.querySelectorAll('.toggle').forEach(el => {
el.addEventListener('click', hide_post);
});
function hide_post(e) {
e.preventDefault();
const toggle = e.target;
const item = e.target.closest('.item');
const details = item.querySelector('.details');
if (details.style.display == 'inline') {
details.style.display = 'none';
toggle.innerHTML = 'Toon meer';
} else {
details.style.display = 'inline';
toggle.innerHTML = 'Toon minder';
}
}
.details { display: none; }
<!-- HTML content generated by your PHP loop... -->
<div class="item">
<h4>Versie: $item->name1</h4>
<div class="details">
<p>Issues in versie: $res->issuesFixedCount1</p>
<p>Afgeronde issues: $roundedIssue1</p>
<p>Nog te verwerken issues: $uns->issuesUnresolvedCount1</p>
</div>
<small>$item->releaseDate1</small>
<a href="#" class="toggle">Toon meer</a>
<a>|</a>
<a href="<?='version/?version=' . $item->name; ?>">Meer info</a>
</div>
<div class="item">
<h4>Versie: $item->name2</h4>
<div class="details">
<p>Issues in versie: $res->issuesFixedCount2</p>
<p>Afgeronde issues: $roundedIssue2</p>
<p>Nog te verwerken issues: $uns->issuesUnresolvedCount2</p>
</div>
<small>$item->releaseDate2</small>
<a href="#" class="toggle">Toon meer</a>
<a>|</a>
<a href="<?='version/?version=' . $item->name; ?>">Meer info</a>
</div>
<div class="item">
<h4>Versie: $item->name2</h4>
<div class="details">
<p>Issues in versie: $res->issuesFixedCount2</p>
<p>Afgeronde issues: $roundedIssue2</p>
<p>Nog te verwerken issues: $uns->issuesUnresolvedCount2</p>
</div>
<small>$item->releaseDate2</small>
<a href="#" class="toggle">Toon meer</a>
<a>|</a>
<a href="<?='version/?version=' . $item->name; ?>">Meer info</a>
</div>
<div class="item">
<h4>Versie: $item->name2</h4>
<div class="details">
<p>Issues in versie: $res->issuesFixedCount2</p>
<p>Afgeronde issues: $roundedIssue2</p>
<p>Nog te verwerken issues: $uns->issuesUnresolvedCount2</p>
</div>
<small>$item->releaseDate2</small>
<a href="#" class="toggle">Toon meer</a>
<a>|</a>
<a href="<?='version/?version=' . $item->name; ?>">Meer info</a>
</div>
You have defined thehide_post
function in a loop, overriding the previoushide_post
function. In your case, it's better to define the function outside the loop and pass a unique parameter to the function. Assuming the item name is unique and does not contain spaces, you can use the following.
function hide_post(postId){
hideshow = document.querySelector('#showMoreLessId_'+postId);
hideshowtext = document.querySelector('#showMoreLess_'+postId);
if (hideshow.style.display == 'inline'){
hideshow.style.display = 'none';
hideshowtext.innerHTML = 'Toon meer';
} else {
hideshow.style.display = 'inline';
hideshowtext.innerHTML = 'Toon minder';
}
}
From your loop in PHP define the like as following,
<a onclick="hide_post('<?= $item->name; ?>')" id="showMoreLess_"<?= $item->name; ?>>Toon meer</a>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.