I have some data attributes set to equal nothing when the page loads. Once a user selects an item from a list item it then populates the data attributes. I want to perform a simple validation that says if one of those attributes is empty then alert an error otherwise carry on.
If I press the button with nothing in the field, I get the error which is fine. But if I then select something I can see in console that the button now has values in the data attributes but I still get the validation error. The same is true if I do the opposite. If I select an item then it passes validation. If I clear the field it should fail validation but it still fails. It seems that the second time around it is still using data from the first click.
$('body').on('click', '#checkout', function(e) {
e.preventDefault();
var user_email = $(this).data('newemail');
var uid = $(this).data('newid');
var first_name = $(this).data('namename');
if(uid !== '') {
//make ajax call
} else {
alert('No values');
}
});
I am setting the data attributes like this when a list item is clicked on:
$( '#checkoutAsCustomer' ).attr('data-newid', uid);
$( '#checkoutAsCustomer' ).attr('data-newemail', user_email);
$( '#checkoutAsCustomer' ).attr('data-newname', first_name);