ajax - Javascript trim whitespace on click -
i have email form field. on click, executes javascript ...
$(document).on('click', '#add_delegate', function() { registrationhelper.added_delegate = true; // var button = $(event.target); var button = $(this); var uri = button.data('url'); if (typeof uri === 'undefined') { return false; } var input = $('#email_search'); var data = {email:input.val()}; data.text().replace(/ /g,''); var spinner = $('#delegate_add_spinner'); spinner.css({display:'inline-block'}); $.ajax({type:'post', url: uri, data:data}).success(function(card) { var html = $(card); var data_id = html.attr('data-id'); var existing_elem = $('.mini_addresscard[data-id=' + data_id + ']'); if (existing_elem.length < 1) { input.popover('hide'); // in seperate timeout because ie8 damn stupid; crashes if run directly settimeout(function () { $('#address_cards').append(html); var last_card = $('#address_cards div.mini_addresscard').last(); //last_card.get(0).innerhtml = card; // html.attr("id", 'sdklfjaklsdjf'); last_card.css({display:'none'}).show('blind'); }, 10); } else { var background = existing_elem.css('background'); existing_elem.css({'background':'#ffffac'}); existing_elem.animate({ 'background-color':'#ebedf1' }, { complete: function() { existing_elem.css({background:background}); } }); // var border_color = existing_elem.css('border-color'); // existing_elem.css({'border-color':'#eff038'}); // existing_elem.animate({'border-color':border_color}); } }).complete(function(data) { spinner.hide(); }).error(function(data) { var input = $('#email_search'); input.popover("destroy"); var error = 'please try again later'; //incase crazy happens if(data.status == "422"){ error = 'you cannot add supervisor.'; } if(data.status == "404" ){ error = 'could not find email address.'; } add_popover(input, error); input.popover('show'); }); return false; });
my goal trim whitespace before ajax request
so can see in code above added line
data.text().replace(/ /g,'');
... renders button useless. in other words button nothing when clicked.
since you're using jquery, why not make use of .trim():
this:
var data = {email:input.val()}; data.text().replace(/ /g,'');
becomes:
var data = {email:$.trim(input.val())};
Comments
Post a Comment