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

242
Vistas
How do i get all inputs from div except empry values for jspdf?

Im using jspdf to create a pdf from some divs i have. There are a lot of inputs,some of them are required,some are not. I need to get only the values that are not empty from my input fields,but i can't make it work,even tho im 90% sure my code should work fine. Here it is:

  var doc = new jsPDF();
    var i=1;
    $("div#home :input[type=text]").each(function(){
      i++;
 var input = $(this);
 if(input.val().lenght>0){
   console.log(input);    
 var text = input.attr('placeholder')+':  '+input.val();
 
 



doc.setFontSize(10);
doc.text(10, (i*8) + (10),text+"\n");
 } 


});
doc.save('a4.pdf')

The code above does not work. when i use it,i get nothing for the pdf. if i remove the .lenght>0 it works,but it gets all the empty values so my pdf goes with a lot of blank spaces. i have tried using input.val().>0,input.val().!=='',and none of them works,they all give me the same result. How can i check properly if my input fields are not null?

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

0

This is probably a typo:

input.val().lenght

should be this instead:

input.val().length

You should see an error message in your browser console, because length was misspelled.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found the answer to my own problem. The code is fine,the problem was in my auto increment variable "i".

      var doc = new jsPDF();
    var i=1;
    $("div#home :input[type=text]").each(function(){
      i++;
 var input = $(this);
 if(input.val().lenght>0){
   console.log(input);    
 var text = input.attr('placeholder')+':  '+input.val();
 
 



doc.setFontSize(10);
doc.text(10, (i*8) + (10),text+"\n");
 } 


});
doc.save('a4.pdf')

As you can see above,my variable was outside the "each" loop that was looping trough the non-empty values of my input fields. The loop was fine,but as the "i++" variable was out of the loop,it was couting empty values and giving me blank spaces between the non-empty ones. So the only thing i needed to do was to move my "i" inside the loop. Yes,pretty stupdid error,but it took me hours to find out.

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