====== Customizing with jQuery ======
**[[http://jquery.com/|jQuery]]** is a popular JavaScript framework that makes it easy to manipulate the HTML Document Object Model (DOM) - which is to say you can modify the user interface. jQuery is already built-in to ViziApps so you don't need to load any library; just start using it.
Some of the examples on this page show you how to tap into the powerful and easy-to-use event processor to catch user actions; how to fetch data from a web API; and how to hide or show fields.
You can reference any ViziApps objects in jQuery such as fields and pages, but you must prepend the ViziApps name with a hashmark, and then complete with the jQuery operator $(). For example:
var vzButtonField = $( '#' + 'buttonAskUserField' );
var buttonId = vzButtonField.attr( 'id' );
===== App startup and deferred code =====
The general format for capturing an event uses the **[[http://api.jquery.com/on/|jQuery .on() operator]]** as follows:
$( //jq_selector// ).on( //event_name//, //event_function// );
Your HTML Header JavaScript code cannot make references to ViziApps field names until the app content has been loaded. Use the following example to defer startup code until your app is ready:
$(document).on('DOMContentLoaded', function() {
setFieldValue( 'startTimeLabel', (new Date()).toLocaleString() );
});
===== Page open events =====
You may want to take some action when any page is first opened, which you can do with the following code. Note that all references to ViziApps objects should happen after the app is ready.
function onPageShowing( pageName, callback ) {
$( '#' + pageName ).on( 'showing', callback );
}
function offPageShowing( pageName, callback ) {
$( '#' + pageName ).off( 'showing', callback );
}
function loginPageHandler() {
offPageShowing( 'loginPage', loginPageHandler );
if ( !confirm( 'Are you sure you want to login?' ) ) {
gotoPage( 'appHomePage' );
}
}
$(document).on('DOMContentLoaded', function() {
onPageShowing( 'loginPage', loginPageHandler );
});
===== Tap events for any field =====
While buttons and tables have no-coding events built-in to their properties, you can use jQuery to turn images, labels, and just about anything into an object that can be tapped. If you create an Image Field called tapThisImage and a Label Field called tapThisLabel, the following code will alert the user to the name of the field that was tapped.
function clickHandler( event ) {
var tappedField = $(event.currentTarget);
alert( 'Clicked: ' + tappedField.attr( 'id' ) );
}
function clickListen( fieldName ) {
$( '#' + fieldName ).click( clickHandler );
}
$(document).on('DOMContentLoaded', function() {
clickListen( 'tapThisLabel' );
clickListen( 'tapThisImage' );
});
===== Simulate a user click =====
You can programatically "click" on any object. One important reason to do this is to trigger a database query from a button that is configured to "Get or send device data via a web data source". This example shows a button that lets the user refresh data on that page, but you also want to show the data the first time the page is shown.
// refreshDataButton is configured to get or send device data
function firstRefreshData() {
offPageShowing( 'showImportantDataPage', firstRefreshData );
$( '#refreshDataButton' ).click();
}
function windowStartup() {
onPageShowing( 'showImportantDataPage', firstRefreshData );
}
$(document).on('DOMContentLoaded', windowStartup );
===== Get data from websites (AJAX) =====
You can use the **[[http://api.jquery.com/jQuery.get/|jQuery .get()]]** function and the more complete **[[http://api.jquery.com/ajaxComplete/|jQuery .ajax()]]** to make requests to websites and receive data. This example uses the **[[http://openweathermap.org/current|OpenWeatherMap]]** website to get the current weather conditions for a specific city. Try creating a button and calling this showTemperature() function from the button's tap event.
// put showTemperature() in HTML Header