Ok, I have really confused myself.
We have this app that concates values and sends them to TA box. I want to determine if the field has a value in it first,
- if it does have a value, add it to TA field
- if it does not have a value/empty, do not add it to the
TA field,
How do i do this?
I want to use this on the blur event[see code example]. I know I need to use .each(), check length and/or look into each field, see if there is a value, if there is, put in TA field
Could someone show me how to accomplish this please?
jsfiddle >> http://jsfiddle.net/justmelat/6JHRT/
HTML
<form>
<p>First name: <input type="text" name="firstname" id="firstname" value="Paul"/></p>
<p>Last name: <input type="text" name="lastname" id="lastname" value="Ryan" /></p>
<p>Street #: <input type="text" name="street_number" id="street_number" value="4605"/></p>
<p>Address: <input type="text" name="address" id="address"/></p>
<p>City: <input type="text" name="city" id="city" /></p>
<p>State: <input type="text" name="state" id="state"/><br />
<span id="myzip">Zip:</span> <input type="text" name="zip" id="zip" />
<br /><br />
<select name="hometype" id="hometype">
<option value="">Select</option>
<option value="SFH">Single Family Home</option>
<option value="Condo">Condo</option>
<option value="Trailer">Trailer</option><br />
</select><br />
<input type="radio" name="ownership" value="own" /> Own
<input type="radio" name="ownership" value="rent" /> Rent
<hr />
<span id="allInfo">All Info:</span> <textarea id="ta_holdAll" rows="10" cols="30"></textarea><div id="charCnt"></div><br /><br />
<input type="button" value="Add to Field" id="addField">
</form>
JQUERY
$(document).ready(function(){
function combineFields(event) {
var mytextareaFld = $('#ta_holdAll');
var fld_1 = $('#firstname').attr('name')+': '+$('#firstname').val();
var fld_2 = $('#lastname').attr('name')+': '+$('#lastname').val();
var fld_3 = $('#street_number').attr('name')+': '+$('#street_number').val();
var hold_all_fields = fld_1 +'\n' + fld_2 +'\n' + fld_3;
//hold_all_fields.each();
mytextareaFld.val(fld_1 +'\n' + fld_2 +'\n' + fld_3);
}
$('#addField').on('click',combineFields);
$('#allInfo').on('click',function(event){
combineFields(event);
var $mytextareaFld = $('#ta_holdAll');
var $outPutCount = $("#charCnt");
var $ofText = " characters of 1000 remaining";
var val = $mytextareaFld.val();
var val2 = $outPutCount.text(val.length).append("<strong>"+$ofText+"</strong>");
});
$("#firstname,#lastname, #street_number").blur(combineFields);
});
Try this — just check if the field exists (has length), and if it doesn’t, use an empty string:
http://jsfiddle.net/6JHRT/3/
For added usefulness, use
jQuery.trim()to wipe out blank spaces:http://jsfiddle.net/6JHRT/4/