Snippets

Dan Bowling Workflow Buttons

Created by Dan Bowling
<!--

1. Insert the script tags in the document.
2. Create a dropdown input with class="tobutton"
3. The form will now submit a hidden input called "submittedbutton" with the value of the button clicked

-->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">
	var submitbutton = {};
	var form = {};

	// find the form and submit button to anchor to
	$("select.tobutton").each(function(){
		form = $(this).closest('form');
		submitbutton = form.find(':submit').first();
	});

	// Create the new submit buttons for each item in the dropdown
	$("select.tobutton > option").each(function(){
		newbutton = $( "<input type='submit' />" );
		newbutton.val($(this).text());
		newbutton.insertAfter(submitbutton);
	});

	// Create a hidden input to store which button was pressed
	form.prepend($( "<input  name=\"submittedbutton\" type=\"hidden\" id=\"submittedbutton\" value=\"\">"));
	
	// Set the hidden input to the value of the button
	$('input[type="submit"]').click(function(e){
	    $("#submittedbutton").val($(this).val());
	});

	// Get rid of the selects we used to populate the form so no one sees them
	$("select.tobutton").each(function(){
		$(this).remove();
	});


</script>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.