Difference between revisions of "JQuery / script cheatsheet"
Jump to navigation
Jump to search
old>Admin |
old>Admin |
||
Line 17: | Line 17: | ||
==== Building multiselect values from existing services ==== | ==== Building multiselect values from existing services ==== | ||
In the following example the field TERRITORY has autocomplete wuth Country values. Output format example: "Denmark, Finland, Sweden" | |||
function split( val ) { | function split( val ) { |
Revision as of 10:07, 7 September 2017
Selecting text for easy copying
var textNode = document.getElementById('IdOfNodeToBeSelected'); if (document.selection) { var range = document.body.createTextRange(); range.moveToElementText(textNode); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents(textNode); selection.removeAllRanges(); selection.addRange(range); }
Building multiselect values from existing services
In the following example the field TERRITORY has autocomplete wuth Country values. Output format example: "Denmark, Finland, Sweden"
function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split( term ).pop(); } $(function() {$("#DATA_TERRITORY").autocomplete({ source: function( request, response ) { $.getJSON( "autocomplete?type=dk.p2e.blanket.form.fields.ajax.FieldAjaxCountry&subtype=0", { term: extractLast( request.term ) }, response ); }, autoFocus: true, min_length: 2, delay: 300, search: function() { // custom minLength var term = extractLast( this.value ); if ( term.length < 2 ) { return false; } }, focus: function() { // prevent value inserted on focus return false; }, select: function( event, ui ) { var terms = split( this.value ); // remove the current input terms.pop(); // add the selected item terms.push( ui.item.value ); // add placeholder to get the comma-and-space at the end terms.push( "" ); this.value = terms.join( ", " ); return false; } });});