.set()
Syntax Variants
Parameters
- name
- the name of a single property or attribute to modify. Unprefixed names set properties, a '$' prefix sets CSS styles and '@' sets attributes. Please see the table above for special properties and other options.
- value
- the value to set. If value is null and name specified an attribute, the attribute will be removed.
If dollar ('$') has been passed as name, the value can contain space-separated CSS class names. If prefixed with a '+' the class will be added,
with a '-' prefix the class will be removed. Without prefix, the class will be toggled.
If value is a function, the
function(oldValue, index, obj)will be invoked for each list element to evaluate the new value:- oldValue
- The old value of the property to be changed, as returned by
get(). For the CSS style names, this is the computed style of the property - index
- The list index of the object owning the property
- obj
- The list element owning the property.
- (callback return value)
- The value to be set.
- properties
- a Object as map containing names as keys and the values to set as map values. See above for the name and value syntax.
- cssClasses
- if set() is invoked with a string as single argument, the name "$" (CSS classes) is assumed and the argument is the
value. See above for CSS syntax.
Instead of a string, you can also specify a
function(oldValue, index, obj)to modify the existing classes. - (return value)
- the list
Description
Modifies the list's elements by setting their properties, attributes, CSS styles and/or CSS classes. You can either supply a single name and value to set only one property, or you can provide an object that contains name/value pairs to describe more than one property. More complex operations can be accomplished by supplying functions as values. They will then be called for each element that will be set.
The name parameter defines what kind of data you are setting. The following name schemes are supported:
| Name Schema | Example | Sets what? | Description |
|---|---|---|---|
| name | innerHTML | Property | A name without prefix of '$' or '@' sets a property of the object. |
| @name | @href | Attribute | Sets the HTML attribute using setAttribute(). In order to stay compatible with Internet Explorer 7 and earlier, you should not set the attributes '@class' and '@style'. Instead use '$' and '$$' as shown below. |
| %name | %phone | Data-Attribute | Sets a data attribute using setAttribute(). Data attributes are attributes whose names start with 'data-'. '%myattr' and '@data-myattr' are equivalent. |
| $name | $fontSize | CSS Property | Sets a style using the element's style object. The syntax for the CSS styles is camel-case (e.g. "$backgroundColor", not "$background-color"). |
| $ | $ | CSS Classes | A simple $ modifies the element's CSS classes using the object's className property. The value is a space-separated list of class names. If prefixed with '-' the class is removed, a '+' prefix adds the class and a class name without prefix toggles the class. The name '$' can also be omitted if set is called with class names as only argument. |
| $$ | $$ | Style | Sets the element's style attribute in a browser-independent way. |
| $$show | $$show | Show/Hide | If true or a number not 0, it will make sure the element is visible by
making sure '$display' is not 'none' and by setting '$visibility' to 'visible'. Please see show() for details. If the value is false or 0, it
will be hidden by setting '$display' to 'none'. |
| $$fade | $$fade | Fade Effect | The name '$$fade' sets the opacity of the element in a browser-independent way. The value must be a number between 0 and 1. '$$fade' will also automatically control the element's 'visibility' style. If the value is 0, the element's visibility will automatically be set to 'hidden'. If the value is larger, the visibility will be set to 'visible'. '$$fade' only works with block elements. |
| $$slide | $$slide | Slide Effect | The name '$$slide' allows a vertical slide-out or slide-in effect. The value must be a number between 0 and 1 and will be used to set the element's '$height'. '$$slide' will also automatically control the element's 'visibility' style. If the value is 0, the element's visibility will automatically be set to 'hidden'. If the value is larger, the visibility will be set to 'visible'. '$$slide' only works with block elements and will not set the element's margin or padding. If you need a margin or padding, you should wrap the elements in a simple <div>. |
| $$scrollX, $$scrollY | $$scrollY | Scroll Coordinates | The names '$$scrollX' and
'$$scrollY' can be used on $(window) to set the scroll coordinates of the document.
The coordinates are specified in pixels, but must not use a 'px' unit postfix. |
Example
Unchecking checkboxes:
$('input.checkbox').set('checked', false);
Example
Changing the innerHTML property of an element:
$('#toc').set('innerHTML', 'Content');
Example
Changing attributes:
$('a.someLinks').set('@href', 'http://www.example.com/');
Example
Removing attributes:
$('a.someLinks').set('@title', null);
Example
Changing styles:
$('.bigText').set('$fontSize', 'x-large');
Example
Adding and removing CSS classes:
$('.myElem').set('$', '+myClass -otherClass');
Example
Toggling a CSS class:
$('.myElem').set('$', 'on');
Example
Shortcut for CSS manipulation:
$('.myElem').set('+myClass -otherClass on');
Example
Making an element transparent:
$('.seeThrough').set('$$fade', 0.5);
Example
Making an element visible. Note that $$fade will set the element's display style to 'block' and visibility style to 'visible'.
$('.myElem').set('$$fade', 1);
Example
Using a map to change several properties:
$('input.checkbox').set({checked: false,
'@title': 'Check this'});
Example
Changing CSS with a map:
$('.importantText').set({$fontSize: 'x-large',
$color: 'black',
$backgroundColor: 'red',
$: '+selected -default'});
Example
You can specify a function as value to modify a value instead of just setting it:
$('h2').set('innerHTML', function(oldValue, index) {
return 'Chapter ' + index + ': ' + oldValue.toUpperCase();
});
See also..
get()retrieves values using the same property syntax.animate()animates values using the same property syntax.toggle()can toggle between two sets of values.dial()allows smooth transitions between two sets of values.
Comments
comments powered by DisqusFunctions
- $() Web
- list.length Web, Util
- $$() Web
- $.getCookie()
- $.loop() Web
- $.off() Web
- $.parseJSON() Web
- $.ready() Web
- $.request() Web
- $.setCookie()
- $.toJSON() Web
- $.wait()
- .add() Web
- .addAfter() Web
- .addBefore() Web
- .addFront() Web
- .animate() Web
- .array() Util
- .call() Util
- .clone() Web
- .collect() Web, Util
- .contains() Util
- .dial() Web
- .each() Web, Util
- .endsWith() Util
- .equals() Util
- .fill() Web
- .filter() Web, Util
- .find() Web, Util
- .findLast() Web, Util
- .get() Web
- .hide() Web
- .ht()
- .intersection() Util
- .is() Web
- .join() Util
- .map() Util
- .merge() Util
- .next() Web
- .not() Web
- .offset()
- .on() Web
- .onChange() Web
- .onClick() Web
- .onFocus() Web
- .onOver() Web
- .only()
- .per() Util
- .reduce() Util
- .remove() Web
- .replace() Web
- .reverse() Util
- .select() Web
- .set() Web
- .show() Web
- .sort() Util
- .startsWith() Util
- .sub() Web, Util
- .text() Web
- .toObject() Util
- .toggle() Web
- .trav() Web
- .trigger() Web
- .uniq() Util
- .unite() Util
- .up() Web
- .values() Web
- EE() Web
- HTML() Web
- M Web, Util
- MINI.getter Web
- MINI.setter Web
- Minified Lists Web, Util
- Promise Web, Util
- _() Util
- _.bind() Util
- _.copyObj() Util
- _.dateAdd() Util
- _.dateClone() Util
- _.dateDiff() Util
- _.dateMidnight() Util
- _.eachObj() Util
- _.escapeHtml() Util
- _.escapeRegExp() Util
- _.extend() Util
- _.filterObj() Util
- _.format() Util
- _.formatHtml() Util
- _.formatValue() Util
- _.isBool() Util
- _.isDate() Util
- _.isEmpty() Util
- _.isFunction() Util
- _.isList() Util
- _.isNumber() Util
- _.isObject() Util
- _.isString() Util
- _.isValue() Util
- _.keys() Util
- _.mapObj() Util
- _.pad() Util
- _.parseDate() Util
- _.parseNumber() Util
- _.partial() Util
- _.promise()
- _.range()
- _.template() Util
- _.toString() Util
- _.trim() Util
- _.values() Util
- define() Web, Util
- promise.always()
- promise.error() Web, Util
- promise.fire()
- promise.stop()
- promise.then() Web
- require() Web, Util
- How to...