### Create new RichFaces Documentation Jira issue

Summary
255 characters left
Description
Environment
This will launch the RichFaces Jira page - to complete your feedback please login if needed, and submit the Jira.

JBoss.org Community Documentation

- Prev
- Next

### 6.14.9. < rich:jQuery > available since 3.0.0

expand all

#### 6.14.9.1. Description

The <rich:jQuery> allows to apply styles and behaviour to DOM objects.

#### 6.14.9.2. Key Features

- Presents jQuery JavaScript framework functionality

- Able to apply onto JSF components and other DOM objects.

- Works without conflicts with prototype.js library

#### 6.14.9.3. Details of Usage

<rich:jQuery> can be used in two main modes:

- as a one-time query applied immediately or on a document ready event

- as a JavaScript function that can be invoked from the JavaScript code

The mode is chosen with "timing" attribute that has the following options:

- `immediate` — applying a query immediately;

- `onload` — applying a query when a document is loaded;

- `onJScall` — applying a query by invoked JavaScript function defined with the "name" attribute.

Definition of the "name" attribute is mandatory when the value of "timing" attribute is "onJScall". If the "name" attribute is defined when "timing" value equals to "immediate" or "onload", the query is applied according to this value, but you still have an opportunity to invoke it by a function name.

The "selector" attribute defines an object or a list of objects. The query is defined with the "query" attribute.

Here is an example of how to highlight odd rows in a table:

    <style>

          .odd {background-color: #FFC;}

    </style>

    <rich:dataTable id="customList" ...>

           ...

    </rich:dataTable>

    <rich:jQuery selector="#customList tr:odd" timing="onload" query="addClass(odd)" />

The "selector" attribute uses defined by w3c consortium syntax for CSS rule selector with some jQuery extensions

Those are typical examples of using selector in the <rich:jQuery> component.

Table 6.262. Examples of using selector

Selector | Comment
--- | ---
p[a] | In a document all "p" tags with "a" tag inside are selected
ul/li | All "li" elements of unordered "ul" lists are selected
p.foo[a] | All "p" tags with "foo" class and inserted "a" tag are selected
input[@name=bar] | All "input" tags with "name" attribute which value is "bar" are selected
input[@type=radio][@checked] | All "input" tags with attribute "type"="radio" and attribute value = "chekced" are selected
p,span,td | All tag elements "p" or"span" or "td" are selected
p#secret | "p" paragraph element with "id" identification = "secret" is selected
p span | "span" tag is a (direct or non-direct) child of "p" tag. If it's necessary, use "p > span" or "p/span" is selected
p[@foo^=bar] | "p" tag containing "foo" attribute with textual value beginning with "bar" word is selected
p[@foo$=bar] | "p" tag containing "foo" attribute with textual value ending with "bar" word is selected
p[@foo*=bar] | "p" tag with "foo" attribute containing substring "bar" in any place is selected
p//span | "span" tag that is a (direct or non-direct) child of "p" tag is selected
p/../span | "span" tag that is a grandchild of "p" tag is selected

In addition, RichFaces allows using either a component id or client id if you apply the query to a JSF component. When you define a selector, RichFaces examines its content and tries to replace the defined in the selector id with a component id if it's found.

For example, you have the following code:

    <h:form id="form">

          <h:panelGrid id="menu">

                <h:graphicImage value="pic1.jpg" />

                <h:graphicImage value="pic2.jpg" />

          </h:panelGrid>

    </h:form>

The actual id of the <h:panelGrid> table in the browser DOM is `"form:menu"`. However, you still can reference to images inside this table using the following selector:

    ...

    <rich:jQuery selector="#menu img" query="..." />

    ...

You can define the exact id in the selector if you want. The following code reference to the same set of a DOM object:

    ...

    <rich:jQuery selector="#form\\:menu img" query="..." />

    ...

Pay attention to double slashes that escape a colon in the id.

In case when the "name" attribute is defined, <rich:jQuery> generates a JavaScript function that might be used from any place of JavaScript code on a page.

There is an example of how to enlarge the picture smoothly on a mouse over event and return back to the normal size on mouse out:

    ...

    <h:graphicImage width="50" value="/images/price.png" onmouseover="enlargePic(this, {pwidth:'60px'})" onmouseout="releasePic(this)"  />

    <h:graphicImage width="50" value="/images/discount.png" onmouseover="enlargePic(this, {pwidth:'100px'})" onmouseout="releasePic(this)"  />

    ...

    <rich:jQuery name="enlargePic" timing="onJScall" query="animate({width:param.pwidth})" />

    <rich:jQuery name="releasePic" timing="onJScall" query="animate({width:'50px'})"/>

    ...

The JavaScript could use two parameters. The first parameter is a replacement for the selector attribute. Thus, you can share the same query, applying it to the different DOM objects. You can use a literal value or a direct reference for an existing DOM object. The second parameter can be used to path the specific value inside the query. The JSON syntax is used for the second parameter. The "param." namespace is used for referencing data inside the parameter value.

<rich:jQuery> adds styles and behavior to the DOM object dynamically. This means if you replace something on a page during an Ajax response, the applied artifacts is overwritten. But you are allowed to apply them again after the Ajax response is complete.

Usually, it could be done with reRendering the <rich:jQuery> components in the same Ajax interaction with the components these queries are applied to. Note, that queries with "timing" attribute set to "onload" are not invoked even if the query is reRendered, because a DOM document is not fully reloaded during the Ajax interaction. If you need to re-applies query with "onload" value of "timing" attribute, define the "name" attribute and invoke the query by name in the "oncomplete" attribute of the Ajax component.

RichFaces includes jQuery JavaScript framework. You can use the futures of jQuery directly without defining the <rich:jQuery> component on a page if it is convenient for you. To start using the jQuery feature on the page, include the library into a page with the following code:

    ...

    <a4j:loadScript src="resource://jquery.js"/>

    ...

Refer to the jQuery documentation for the right syntax. Remember to use `jQuery()` function instead of `$()`, as soon as jQuery works without conflicts with `prototype.js`.

#### 6.14.9.4. Reference Data

Table of <rich:jQuery> attributes.

Table 6.263. Component Identification Parameters

Name | Value
--- | ---
component-type | org.richfaces.JQuery
component-class | org.richfaces.component.html.HtmlJQuery
component-family | org.richfaces.JQuery
renderer-type | org.richfaces.JQueryRenderer
tag-class | org.richfaces.taglib.JQueryTag

#### 6.14.9.5. Relevant Resources Links

Visit the jQuery page at RichFaces LiveDemo for examples of component usage and their sources.

More information about jQuery framework you can find injQuery official documentation.

See also:

- "Using jQuery with Other Libraries" in jQuery official documentation.

- Prev6.14.8. <rich:messages> available sinc...
- Top of page
- Front page
- NextChapter 7. IDE Support