Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to display two objects at a time from an array of object when I click a button named show more items. How can I accomplish this. I have posted the codes here for anyone who can help. Also when there is no more items left, the button's innerHTML can be change to no more items to show. Thanks.

What I have tried:

HTML
<!DOCTYPE html>
<html>
<body>
<div id="output" style='padding:30px'>
<h1 id='h1'>Hello This is my text</h1>
</div>
<button id='btn' data-id='' onclick='addText()'>Add text</button>
</body>
<script type='text/javascript'>
var people = [
{id:1, name: 'Tom', job: 'Software developer', age: 33},
{id:2, name: 'Jane', job: 'Software developer', age: 36},
{id:3, name: 'Dick', job: 'Software developer', age: 38},
{id:4 name: 'Jenny', job: 'Software developer', age: 41},
{id:5, name: 'Harry', job: 'Software developer', age: 36}
{id:6, name: 'Joe', job: 'Software developer', age: 46}
];

function addText(){
for(var i = 0; i < people.length; i++){
const par = document.getElementById('h1');
const text = people[i].name + '' + people[i].job + '' people[i].age;
par.insertAdjacentHTML('afterend', text);
}
}
</script>
</html>
Posted
Updated 4-Jul-22 6:29am
v2
Comments
Member 15627495 4-Jul-22 12:22pm    
people[i].job .... people[i+1].job

for(var i=0;i < people.length ; i=i+2 ) {




}

1 solution

Hey buddy, I hope this helps I tested it out and works the way you requested... Try it out =)

<!DOCTYPE html>
<html>
<body>
  <div id="output" style='padding:30px'>
    <h1 id='h1'>Hello This is my text</h1>
  </div>
    <button id='btn' data-id=''>Add text</button>
  <script>
  var people = [
  {id:1, name: 'Tom', job: 'Software developer', age: 33},
  {id:2, name: 'Jane', job: 'Software developer', age: 36},
  {id:3, name: 'Dick', job: 'Software developer', age: 38},
  {id:4, name: 'Jenny', job: 'Software developer', age: 41},
  {id:5, name: 'Harry', job: 'Software developer', age: 36},
  {id:6, name: 'Joe', job: 'Software developer', age: 46}
  ];

  let objs = 0
  let btn = document.getElementById('btn')
  btn.addEventListener('click', () => addText())

  function addText() {
    let output = document.getElementById('output')
    let max = objs + 2
    for (let i = objs; i < people.length && i < max; i++) {
      let p = document.createElement('p')
      p.innerHTML = people[i].name + '' + people[i].job + '' + people[i].age
      output.appendChild(p)
      objs++
    }
    if (objs >= people.length - 1) {
      btn.innerHTML = 'no more items left'
    }
  }
  </script>
</body>
</html>
 
Share this answer
 
Comments
Andy R 2022 5-Jul-22 10:30am    
Hi, Cordell thanks for your reply, I appreciate it. Yes I test the solution you have given and it did work. Thanks a lot for the help. So far the help I have gotten here is better, way better than on Stackoverflow. I will also try my best to assist anyone who needs help. Thanks much and great solution.
cordell bonnieux 5-Jul-22 12:48pm    
No problem, I'm glad I could help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900