Created
January 28, 2016 10:47
-
-
Save ricoSaw/abd598a841f264649c62 to your computer and use it in GitHub Desktop.
builds the dynamic form and fills with data via JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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