Skip to content
the poor coder

onclick show 10 elements

Poor Man
Published
Reading time
2 min read

How to Show 10 Elements on Click in HTML#

If you want to show 10 elements on click, you can use the onclick event handler in HTML. This event handler is used to trigger a function when an element is clicked. Here is an example of how you can show 10 elements on click:

Text

<!DOCTYPE html>
<html>
<head>
<title>Show 10 Elements on Click</title>
</head>
<body>

<button onclick="showTenElements()">Show 10 Elements</button>

<div id="container">
<ul id="elements">
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
<li>Element 4</li>
<li>Element 5</li>
<li>Element 6</li>
<li>Element 7</li>
<li>Element 8</li>
<li>Element 9</li>
<li>Element 10</li>
</ul>
</div>

<script type="text/javascript">
function showTenElements() {
var elements = document.getElementById("elements").getElementsByTagName("li");

for (var i = 0; i < elements.length; i++) {
if (i < 10) {
elements[i].style.display = "block";
}
else {
elements[i].style.display = "none";
}
}
}
</script>

</body>
</html>

Explanation#

In the above code, we have a button with an onclick event handler that calls the showTenElements function. The function gets all the li elements inside the ul with the id "elements" using the getElementsByTagName method. It then loops through each element and checks if its index is less than 10. If it is, it sets the display property of the element to "block" to show it. If it is not, it sets the display property to "none" to hide it.

The CSS property "display" controls how an element is displayed on a web page. The value "block" makes an element take up the full width available and starts on a new line. The value "none" hides an element and removes it from the flow of the page.

This is just one way of showing 10 elements on click. You can use different HTML elements, CSS properties and JavaScript methods to achieve the same result. For example, you can use the jQuery library to simplify the code:

Text

$(document).ready(function() {
    $("#show-elements").click(function() {
        $("li:lt(10)").toggle();
    });
});

This code uses the jQuery library to select and toggle the display of the first 10 li elements when a button with the id "show-elements" is clicked.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC