Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
How to count <div> with Javascript

Hey guys I have a problem with my code. I have one main component where I have different sub components like this:

<template>
 <div id="container">

  <div v-if=condition>
   Component1
  </div>

  <div v-if=condition>
   Component2
  </div>

  <div v-if=condition>
   Component3
  </div>

  <div v-if=condition>
   Component 4
  </div>
 </div>
</template>

The v-if is because i only want to render one component in the frontend step by step. So when I start my application, I only show the first component, when I click on next, I show the second component and so on.

Now I want a page indicator, so I have to count the child div tags of "container". For that I already tried these options:

  1. var count = $("#container div").length; //returns 0

  2. var count2 = $("#container").children().length; //returns 2

That results are false because the real result would be 4 because there are 4 children div. I think the problem is, that the other components are not all rendered but how can I solve this problem?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Given that you're using Vue, I would use a counter in your data object to keep track of where you are, then just increment it when you click a button to proceed to the next step:

new Vue({
  el: "#app",
  data: {
    currentStep: 1
  },
  methods: {
    increaseStep() {
      if (this.currentStep > 4) {
        return;
      }
      this.currentStep++;
    }
   }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="container">

    <div v-if="currentStep >= 1">Step 1</div>
    <div v-if="currentStep >= 2">Step 2</div>
    <div v-if="currentStep >= 3">Step 3</div>
    <div v-if="currentStep >= 4">Step 4</div>
    
    <button v-on:click="increaseStep">Next step</button>

  </div>


</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

var count = $("#container div").length;
document.write(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="container">

  <div v-if=condition>
   Component1
  </div>

  <div v-if=condition>
   Component2
  </div>

  <div v-if=condition>
   Component3
  </div>

  <div v-if=condition>
   Component 4
  </div>
 </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Checked the same, but it seems working for me.

Probably you need to check the length after the document is ready. I mean in the mounted lifecycle method.

But using jQuery inside another library is not a better way of doing it unless there is no alternative.

So in this case, you can use native JS to find the number of div's inside the container.

$(document).ready(function(){

  console.log($("#container div").length);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">

  <div v-if=condition>
   Component1
  </div>

  <div v-if=condition>
   Component2
  </div>

  <div v-if=condition>
   Component3
  </div>

  <div v-if=condition>
   Component 4
  </div>
 </div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda