Skip to content

Instantly share code, notes, and snippets.

@ricoSaw
Created January 28, 2016 10:47
Show Gist options
  • Save ricoSaw/abd598a841f264649c62 to your computer and use it in GitHub Desktop.
Save ricoSaw/abd598a841f264649c62 to your computer and use it in GitHub Desktop.
builds the dynamic form and fills with data via JavaScript
$(document).ready(function () {
// click event-listener for discounted link
$('#marketing-discounted-link').click(function(){
// data from data attributes
var data = $('#marketing-discounted-data').data();
var url = $(this).attr('href');
var dynForm = buildForm(data, url);
dynForm.submit();
return false;
});
// builds the dynamic form and fills with data
// this form will send when the discounted link was clicked
function buildForm(data, url){
var form = $('<form>', {
'action': url,
'method': 'post'
});
$.each(data, function(key, value) {
var newKey = key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
form.append( $('<input>').attr('name', newKey).val(value) );
});
return form;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment