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?
CodePudding user response:
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.
CodePudding user response:
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.