Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!