I have several lines of code that perform similar functions (they take the values from inputs and appends a line of code). Is there a way that I could simplify these?
//======Name
var name = $('input[name=Name]').val();
if (name){
$('.results').append('<div class="item">' + '<p>Your name is ' + name + '.</p>' + '</div>');}
//======Age
var age = $('input[name=YourAge]:checked').val();
if (age){
$('.results').append('<div class="item">' + '<p>Your age is ' + age + '.</p>' + '</div>');}
//======Occupation
var occupation = $('input[name=Occupation]').val();
if(occupation){
$('.results').append('<div class="item">' + '<p>Your occupation is ' + occupation + '.</p>' + '</div>');}
Look at what's the same in each piece, and what's different. Turn the similar parts into the body of a function, and make the different parts the parameters to the function:
Then, use it like:
Notice how each piece of code only differs by the selector used, and the "label" printed to describe the item. Those two pieces become the parameters, and after you rename some variables, you'll have a generalized function you can use.