javascript - Trim strings before using map? -
i want input values page user has made , want store in array make ajax-call process data. i'm doing way:
$('#save').click(function(){ var data = $('*[data-array]').map(function(idx, elem) { $.trim(elem); return $(elem).val(); }).get();
the problem won't trim strings before creating array. seems $.trim
doesn't apply? example if type abc____________
(_
being whitespace, have write here show whitespace demonstration) i'll result: abc_
(with 1 whitespace after abc)
you have trim value
$('#save').click(function(){ var data = $('*[data-array]').map(function(idx, elem) { $(elem).val($.trim($(elem).val())); return $(elem).val(); }).get();
$('#save').click(function(){ var data = $('*[data-array]').map(function(idx, elem) { $(elem).val($.trim($(elem).val())); return $(elem).val(); }).get() console.log(data); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <input data-array/> <input data-array/> <input data-array/> <input type="button" id="save" value="save"/>
Comments
Post a Comment