Loading...
 
Add JQuery Javascript code

Plugin JQ

This wiki plugin, introduced in Tiki3, allows jQuery JavaScript code to be inserted on a page.

1. Requirements

The jQuery feature must be enabled at Control Panels > Features > Experimental > jQuery (checkbox).

2. Parameters

Introduced in Tiki3.
Preferences required: wikiplugin_jq
Parameters Accepted Values Description Default Since
(body of plugin) JavaScript code
lang Language to apply JQuery to 13.0
nojquery Optional markup for when JavaScript is off 3.0
notonready Do not execute on document ready (execute inline) 3.0


 Syntax changed since Tiki 7

Keep in mind that default syntax for tracker fields changed since Tiki7, so that when referring them from within plugin jQuery, you have to update the syntax.

Tracker field IDs and names have changed to improve consistency: all tracker fields now use ins_XX , where XX is the field Id.

For more information, read: http://doc.tiki.org/Tiki7#Upgrade_notes

And basic syntax changed after Tiki6:

A Simple jQuery line of code looks like this:
Standard jQuery
$("p").show("slow");


When using the Tiki JQ Plugin in old versions 3 to 5, you needed to start you code with jq:
Tiki jQuery Call
$jq("p").show("slow");



3. Examples

3.1. Simple one

A Simple jQuery line of code looks like this:
Standard jQuery
$("p").show("slow");


3.2. Adding other jQuery plugins to your Tiki

This plugin allows you to use other jQuery plugins to your Tiki.
Let's see how to add jShowOff jQuery plugin as an example:

From http://zukakakina.com/jShowOff+demo



3.3. Special notes:

When following above guide, you can only call the included script from the getScript handler. That means, you cannot just include the script under look and feel area, and then use that script from the JQ plugin - the order of loading the script and using it, will conflict. In short: Call your custom script from the getScript handler.

When inserting the js to include the script, it HAS to be wrapped in
{jq} and {/jq}


JQplugin won't substitute to the pretty tracker form, field or design you are require to do. In one sentence; With JQplugin you can add functionnality to an already working pretty tracker.

A good practice is to create your tracker, your wiki page do the design you need and test that everything is working fine. Then without touching what's has been done already you can add the JQplugin at the bottom of the wiki page.

Fields type are a little different than the one used in the tracker or the pretty tracker. For instance a tracker field id 1 which is a text field should specify as ins_1 . If it is a textarea it is ins_1 also, If a date you have to specify (if need all of them) ins_1Day , ins_1Month , ins_1year . Note also that the syntax use the html form element. Input is an input field, select if for selectable options, etc... To check what is exactly required to use JQ with your tracker you can look at the source of the page (use Firebug or any other tool) and use the exact syntax.

3.4. Prefill tracker fields

3.4.1. Directly at the wiki page

Basic Example A
For instance, if we want to pre-load some values in a tracker plugin form, where you have text field id's 20, 21, 22, and a textarea field id 23, and you want some text to be preloaded in the fields 22 and 23 of your form, we can use something like the syntax below (in a tiki 3, 4, 5 or 6 site as such):

jQuery code since Tiki7+ to add in the wiki page where the tracker plugin call is located
{$f_22}
{JQ()}
$("#ins_22").val("foo or bar");  // to prefill a text field, option1
$("#ins_23").val("first line of text\nsecond line\nthird line");  // to prefill a text area
$("#ins_3572").text("Text"); // to prefill a text field, option 2
$('[name="ins[3496]"]')[1].checked=true; // to prefill a checkbox
$("select[name=ins_91]").val("foo") // to prefill a drop down field
{JQ}


And if you want those values to be pre-filled in your form only when there is not previously saved content in that tracker field for that item (remember you can edit tracker items also from wiki pages), the syntax is:

jQuery code since Tiki7+ to add in the wiki page where the tracker plugin call is located
{JQ()}
if (!$("#ins_22").val()) { $("#ins_22").val("foo or bar"); }
if (!$("#ins_23").val()) { $("#ins_23").val("first line of text\nsecond line\nthird line"); }
if (!$("#ins_3572").val()) { $("#ins_3572").text("Text"); }
if (!$('[name="ins[3496]"]')[1].checked) { $('[name="ins[3496]"]')[1].checked=true; }
if (!$("select[name=ins_91]")[0].selectedIndex) { $("select[name=ins_91]").val("foo") }
{JQ}


Basic Example B

You can use some value defined in some other html input in the same wiki page:
{HTML()}<input type="hidden" name="testingr" id="testingr" value="temp3" />{HTML}


In this case below, field 91 is a dropdown field:
{JQ()}  if (!$("select[name=ins_91]")[0].selectedIndex) $("select[name=ins_91]").val($("#testingr").val()) {JQ}


Basic Example C
Using the id & name "routput" generated by a PluginR call:

{DIV(class="hidden")}{R()}a<-1:10; cat(mean(a));{R}{DIV}

{JQ()}  if (!$("#other_ins_92").val()) $("#other_ins_92").val($("#routput").text()){JQ}


Basic Example D
You can use some value defined in some other html input in the same wiki page:
{HTML()}<input type="hidden" name="mydate" id="mydate" value="{display name="tracker_field_mydate" format="date"}" />{HTML}


In this case below, field 46 is a text field, and we want it hidden for the end user, that's why we end up appending the last jquery part .parents(".form-group").hide():
{JQ()}  if (!$("#ins_46")) $("#ins_46").val($("#mydate").val()).parents(".form-group").hide() ; {JQ}


3.4.2. By means of a wiki page used as a smarty template ('Pretty trackers')

This case is needed when you are adding some prefill by means of a smarty template which resides ona wiki page.

This is very useful when you use PluginR in advanced mode with Pretty Trackers, for instance, to mix R code and html forms to collect parameter data managed by tiki trackers. The smarty template allows inserting data from tracker item fields in R scripts.

Example:
Prefilling tracker item fields since Tiki7+ from smarty templates in wiki pages
{wikiplugin _name=jq}if (!$("#ins_22").val()) $("#ins_22").val("foo or bar"); {/wikiplugin}

{wikiplugin _name=jq}if (!$("#ins_23").val()) $("#ins_23").val("first line of text\nsecond line\nthird line"); {/wikiplugin}
{wiki}
* field 22 is: {$f_22}
* field 23 is: {$f_23}
{/wiki}


3.5. Pre-fill with css styles

The pre-filling performed by jQuery can be done with some style, to indicate the user that this content comes from a suggestion when the field in the tracker form is empty (yet) for that item.

Thus, in the first example above ("Prefill tracker fields > Directly at the wiki page"), if you want those values to be prefilled in your form only when there is not previously saved content in that tracker field for that item AND with some css style like background color light yellow, font color grey and in italics, the syntax is:

jQuery code since Tiki7+ to add in the wiki page where the tracker plugin call is located
{JQ()}
if (!$("#ins_22").val()) { $("#ins_22").val("foo or bar"); 
$("#ins_22")
 .css('background-color', 'lightyellow')
 .css('font-style', 'italic')
 .css('color', 'grey')
 .focus(function() {
      $(this)
        .css('background-color', '')
        .css('font-style', '')
        .css('color', '');
    });
}
if (!$("#ins_23").val()) { $("#ins_23").val("first line of text\nsecond line\nthird line"); 
$("#ins_23")
 .css('background-color', 'lightyellow')
 .css('font-style', 'italic')
 .css('color', 'grey')
 .focus(function() {
      $(this)
        .css('background-color', '')
        .css('font-style', '')
        .css('color', '');
    });
}
{JQ}

3.6. Conditional display of fields

3.6.1. Example 1

Imagine that you have a form with 15 fields. Field number 10 is a checkbox checked by default, and you want that when field 10 is checked, field 11 (a text area, for instance) is shown. But when field 10 is not checked, fields 12, 13, 14 & 15 are shown instead.

You can get the following behavior with the following jQuery syntax:

Conditional display of fields since Tiki 7+ in the wiki page where the tracker plugin call is located
{JQ()}
$("input[name=ins_10]").click(function(){
    if (this.checked) {
        $("input[name=ins_10]").parents("tr:first + tr ~ tr").fadeOut('fast');
        $("input[name=ins_10]").parents("tr:first + tr ~ tr:first").fadeIn('fast');
    } else {
        $("input[name=ins_10]").parents("tr:first + tr ~ tr").fadeIn('fast');
        $("input[name=ins_10]").parents("tr:first + tr ~ tr:first").fadeOut('fast');
    }
});
$("input[name=ins_10]").parents("tr:first + tr ~ tr").hide();
$("input[name=ins_10]").parents("tr:first + tr ~ tr:first").show();
{JQ}


Explanation "for humans":
.parents("tr:first + tr ~ tr") means: the first parent which is a <tr>, go to the next <tr> ("+ tr"), then pick all following <tr> siblings ("~ tr")

"tr:first + tr ~ tr:first" is much the same but only select the first sibling

3.6.2. Example 2

Imagine that you have a form with 15 fields, where you always want to show the first 9 fields, and the last two (14 and 15), and field 10 controls which fields are shown from the numbers 11, 12, 13. Field number 10 is a drop down box, with possible values: empty value (default), TRUE and FALSE. And imagine that you want that when field 10 is set as FALSE, field 11 (a text area, for instance) is shown but not fields 12 (text field), and 13 (drop down with other text field). But when field 10 is set as TRUE, field 11 is hidden, and fields 12 and 13 are shown. And when field 10 is not set (empty value, the default option), fields 11, 12 & 13 are all hidden.

You can get the following behavior with the following jQuery syntax:

Only the text fields for typing are hidden, not the whole row of the html form
{JQ()}
$("select[name=ins_10]").change(function(){
    if ($(this).val() === 'FALSE') {
         $("input[name=ins_11]").hide();
         $("input[name=ins_12]").show();
         $("select[name=ins_13]").show();
         $("input[name=other_ins_13]").show();
    } else if ($(this).val() === 'TRUE') {
         $("input[name=ins_11]").show();
         $("input[name=ins_12]").hide();
         $("select[name=ins_13]").hide();
         $("input[name=other_ins_13]").hide();
    } else {
         $("input[name=ins_11]").hide();
         $("input[name=ins_12]").hide();
         $("select[name=ins_13]").hide();
         $("input[name=other_ins_13]").hide();
    }
});
$("input[name=ins_11]").hide();
$("input[name=ins_12]").hide();
$("select[name=ins_13]").hide();
$("input[name=other_ins_13]").hide();
{JQ}


3.6.3. Example 3

This is the same example as Example 2 but in this case, you want to hide the whole row from the table in the form (and not just hiding the field for typing).

You can get the following behaviour with the following jQuery syntax:

The whole row of the form is hidden for the tracker fields indicated
{JQ()}
$("select[name=ins_10]").change(function(){
    if ($(this).val() === 'FALSE') {
         $("input[name=ins_11]").parents("tr:first").hide();
         $("input[name=ins_12]").parents("tr:first").show();
         $("input[name=other_ins_13]").parents("tr:first").show();
    } else if ($(this).val() === 'TRUE') {
         $("input[name=ins_11]").parents("tr:first").show();
         $("input[name=ins_12]").parents("tr:first").hide();
         $("input[name=other_ins_13]").parents("tr:first").hide();
    } else {
         $("input[name=ins_11]").parents("tr:first").hide();
         $("input[name=ins_12]").parents("tr:first").hide();
         $("input[name=other_ins_13]").parents("tr:first").hide();
    }
});
$("input[name=ins_11]").parents("tr:first").hide();
$("input[name=ins_12]").parents("tr:first").hide();
$("input[name=other_ins_13]").parents("tr:first").hide();
{JQ}


3.6.4. Example 4

This is an example that shows how to iterate over a collection of input textboxes in a tracker. First the textboxes is selected and an event that responds to keyboard input is set for each textbox. When text is entered, the textbox below the active one, will be shown. The process repeats until there are no more hidden textboxes left to show. NB. this code will only work with 1 tracker on the page. If you include more trackers on the same page, the script needs to be modified a bit. (see Example 4.1 below)

The whole row of the form is hidden for the tracker fields indicated
{JQ()}
$(".wikiplugin_tracker  input").each(function() {
    $(this).keyup(function() {
        $(this).parents("tr").next().show("slow", "swing");
 
          }).parents("tr").hide();
});
$(".wikiplugin_tracker  input:first").parents("tr").show();
{JQ}


3.6.5. Example 4.1

This example shows how to achieve the same functionality as above, but with two different trackers on the same page.
The whole row of the form is hidden for the tracker fields indicated
{JQ()}

$("#editItemForm1 input").each(function() {
    $(this).keyup(function() {
        $(this).parents("tr").next().show("slow", "swing");
 
          }).parents("tr").hide();
});
$("#editItemForm1 input:first[type=text]").parents("tr").show();

$("#editItemForm2 input").each(function() {
    $(this).keyup(function() {
        $(this).parents("tr").next().show("slow", "swing");
 
          }).parents("tr").hide();
});
$("#editItemForm2 input:first[type=text]").parents("tr").show();
{JQ}

It is worth noting that #editItemForm1 is the form containing the first tracker and #editItemForm2 is the form containing the second tracker.

3.6.6. Example 5

Hide a field shown in the tracker form which does not contain any text field or checkbox, etc. That's the case of the autoincrement field, which is shown in the PluginTracker form at item creation time, but it could be very well hidden for the sake of simplicity.

If the autoincrement tracker field has id 149, then the code is:

The whole row of the form is hidden for the autoincrement tracker field indicated
{JQ()}
	$("label[for='ins_149']").parents("tr:first").hide();
{JQ}


3.6.7. Example 6

This example has 1 radio button fields (field 38) as 0=no, or 1=yes. And when the user presses in the option 1=yes, there are 3 fields shown below (fields 39, 46, 47).

In addition, the field with the radio button has some css background color provided, which matches the background color of the fields shown when the option "yes" is selected. And when selecting "no", there is an attempt to clear any values which have been provided already to the fields that are being hidden.

Code
{JQ()}
$("input[name=ins_38]").change(function(){
    if ($(this).val() === '0') {
        $("input[name=ins_39]").parents("tr:first").hide("slow");
        $("input[name=ins_39]").val("");
        $("input[name=ins_46]").parents("tr:first").hide("slow");
        $("input[name=ins_46]").val("");
        $("input[name=ins_47]").parents("tr:first").hide("slow");
        $("input[name=ins_47]").val("");
    } else if ($(this).val() === '1') {
        $("input[name=ins_39]").parents("tr:first").show("slow");
        $("input[name=ins_46]").parents("tr:first").show("slow");
        $("input[name=ins_47]").parents("tr:first").show("slow");
        $("input[name=ins_39]").parents("tr:first")
            .css('background-color', 'lightyellow')
            .css('font-style', 'italic')
            .css('color', 'grey')
            .focus(function() {
                $(this)
                    .css('background-color', '')
                    .css('font-style', '')
                    .css('color', '');
            });
        $("input[name=ins_46]").parents("tr:first")
            .css('background-color', 'lightyellow')
            .css('font-style', 'italic')
            .css('color', 'grey')
            .focus(function() {
                $(this)
                    .css('background-color', '')
                    .css('font-style', '')
                    .css('color', '');
            });
        $("input[name=ins_47]").parents("tr:first")
            .css('background-color', 'lightyellow')
            .css('font-style', 'italic')
            .css('color', 'grey')
            .focus(function() {
                $(this)
                    .css('background-color', '')
                    .css('font-style', '')
                    .css('color', '');
            });
    } else {
        $("input[name=ins_39]").parents("tr:first").hide("slow");
        $("input[name=ins_39]").val("");
        $("input[name=ins_46]").parents("tr:first").hide("slow");
        $("input[name=ins_46]").val("");
        $("input[name=ins_47]").parents("tr:first").hide("slow");
        $("input[name=ins_47]").val("");
    }
});
$("input[name=ins_39]").parents("tr:first").hide();
$("input[name=ins_46]").parents("tr:first").hide();
$("input[name=ins_47]").parents("tr:first").hide();
$("input[name=ins_38]").parents("tr:first")
    .css('background-color', 'lightyellow')
    .focus(function() {
        $(this)
            .css('background-color', '');
    });
{JQ}


3.6.8. Other examples: Dynamic (pretty) trackers

See http://doc.tiki.org/Pretty+Tracker#Dynamic_Pretty_Trackers to learn other examples of avdanced usage of this PluginJQ plugin with trackers.

More jQuery tips & tricks

For more tips and tricks using jQuery, visit:
http://api.jquery.com/

For more information on jQuery selectors:
http://api.jquery.com/category/selectors/

doc.tiki.org


Bootstrap

AdminGuide

UserGuide

Keywords

Keywords serve as "hubs" for navigation within the Tiki documentation. They correspond to development keywords (bug reports and feature requests):

Accessibility (WAI and 508)
Accounting (7.x)
Articles and Submissions
Backlinks
Banners
Batch (6.x)
BigBlueButton audio/video/chat/screensharing (5.x)
Blog
Bookmark
Browser Compatibility
Link Cache
Calendar
Category
Chat
Clean URLs
Comments
Communication Center
Compression (gzip)
Contacts (Address Book)
Contact us
Content Templates
Contribution (2.x)
Cookie
Copyright
Credit (6.x)
Custom Home and Group Home Page
Date and Time
Debugger Console
Directory of hyperlinks
Documentation link from Tiki to doc.tiki.org (Help System)
Docs 8.x
Draw 7.x
Dynamic Content
Dynamic Variable
External Authentication
FAQ
Featured links
File Gallery
Forum
Friendship Network (Community)
Gmap Google maps
Groups
Hotword
HTML Page
i18n (Multilingual, l10n, Babelfish)
Image Gallery
Import-Export
Install
Integrator
Interoperability
Inter-User Messages
InterTiki
Kaltura video management (4.x)
Karma
Live Support
Login
Logs (system & action)
Look and Feel
Lost edit protection
Mail-in
Map with Mapserver
Menu
Meta Tags
Mobile Tiki and Voice Tiki
Mods
Module
MultiTiki
MyTiki
Newsletter
Notepad
Payment
Performance Speed / Load
Permissions
Platform independence (Linux-Apache, Windows/IIS, Mac, BSD)
Polls
Profiles
Profile Manager
Report
Toolbar
Quiz
Rating
Feeds
Score
Search engine optimization
Search
Search and Replace
Security
Semantic links (3.x)
Shadowbox
Shadow Layers
Share
Shopping cart
Shoutbox
Slideshow
Smiley
Social Networks
Spam protection (Anti-bot CATPCHA)
Spellcheck
Spreadsheet
Stats
Surveys
Tags (2.x)
Task
Tell a Friend, alert + Social Bookmarking
TikiTests (2.x)
Theme CSS & Smarty
Trackers
Transitions (5.x)
TRIM
User Administration including registration and banning
User Files
User Menu
Watch
WebHelp
WebDAV (5.x)
Webmail
Web Services
Wiki 3D
Wiki History, page rename, etc
Wiki Page Staging and Approval (2.x)
Wiki Plugin extends basic syntax
Wiki Syntax
Wiki structure (book and table of content)
Workspace
WSOD
WYSIWYCA
WYSIWYG (2.x)
XMLRPC


Tiki Newsletter

Delivered fresh to your email inbox!
Newsletter subscribe icon
Don't miss major announcements and other news!
Contribute to Tiki