### 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.65. < rich:jQuery >

### 6.65.1. Description

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

### 6.65.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

Table 6.316. rich : jQuery attributes

Attribute Name | Description
--- | ---
binding | The attribute takes a value-binding expression for a component property of a backing bean
id | Every component may have a unique id that is automatically created if omitted
name | The name of a function that will be generated to execute a query. The "name" attribute is required if "timing" attribute equals to "onJScall"
query | The query string that is executed for a given selector.
rendered | If "false", this component is not rendered
selector | Selector for query. The "selector" attribute uses defined by w3c consortium syntax for CSS rule selector with some jQuery extensions.
timing | The attribute that defines when to perform the query. The possible values are "immediate","onload" and "onJScall". "immediate" performs the query right away. "onload" adds the task to the time when a document is loaded (the DOM tree is created). "onJScall" allows to invoke the query by Javascipt function name defined with "name" attribute. The default value is "immediate".

Table 6.317. 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.65.3. Creating the Component with a Page Tag

To create the simplest variant on a page, use the following syntax:

Example:

    ...

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

    ...

### 6.65.4. Creating the Component Dynamically Using Java

Example:

    import org.richfaces.component.html.HtmlJQuery;

    ...

    HtmlJQuery myJQuery = new HtmlJQuery();

    ...

### 6.65.5. 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:

Example:

    ...

    <style>

        .odd {

              background-color: #FFC;

        }

    </style>

    ...

    ...

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

            ...

        </rich:table>

        ...

        <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.318. 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 ... />

                <h:graphicImage ... />

                ...

            </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 onmouseover="enlargePic(this)" width="50" value="/images/price.png"

                    onmouseover="enlargePic(this, {pwidth:'60px'})" onmouseover="releasePic(this)"  />

        <h:graphicImage onmouseover="enlargePic(this)" width="50" value="/images/discount.png"

                    onmouseover="enlargePic(this, {pwidth:'100px'})" onmouseover="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.65.6. Look-and-Feel Customization

<rich:jQuery> has no skin parameters and custom style classes, as the component isn't visual.

### 6.65.7. Relevant Resources Links

More information about jQuery framework and its features you can read here.

How to use jQuery with other libraries see here.

Some additional information about usage of component can be found on its LiveDemo.

- Prev6.64. < rich:insert >
- Top of page
- Front page
- Next6.66. < rich:listShuttle >