Skip to content

Instantly share code, notes, and snippets.

@klhall1987
Created November 29, 2016 15:42
Show Gist options
  • Save klhall1987/a39c3825eda0629ab593995e29f44763 to your computer and use it in GitHub Desktop.
Save klhall1987/a39c3825eda0629ab593995e29f44763 to your computer and use it in GitHub Desktop.
This Gist shows how to use JS to get and modify form data.
jQuery( document ).ready( function() {
var runCustomJS = Marionette.Object.extend( {
initialize: function() {
this.listenTo( Backbone.Radio.channel( 'form' ), 'render:view', this.editFieldData );
},
/*
* In this function you can do things like:
* modify the form data
* modify field data
* etc.
*/
editFieldData: function( view ) {
//Get form data.
var formModel = view.model;
var fieldCollection = formModel.get( 'fields' );
//To update a specific field by key.
var emailField = fieldCollection.findWhere( { key: 'email' } );
emailField.set( 'value', 'someValue' );
//Tell the field to re-render.
emailField.trigger( 'reRender' );
//Update a field by ID.
var htmlField = fieldCollection.get( 12 );
htmlField.set( 'value', 'someValue' );
//Tell the field to re-render.
htmlField.trigger( 'reRender' );
}
});
new runCustomJS();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment