attr()

attr(attributeName​) : String

Returns the value of the specified attribute of an HTML element which can be:

  • The first element in a set of elements that match the selector of a script (see results).
  • One element that matches the selector of a script that runs for "Each matched element" (see this and Setting the scope of a script).
  • The first element in a set of elements returned by a query in the template (see query()).

attributeName

String; the name of the attribute.

Examples

This script - with the selector img - stores the source of the first image in a variable.

​var src = results.attr("src");​

​The following script looks up an image with the ID #image1 and stores its background color in a variable.

var imgURL = query("#image1").attr("src");

attr(attributeName, valu​e)

Sets the value of the specified attribute of one HTML element or of each element in a result set.

attributeName

String; the name of the attribute.

value

String; value for the attribute.

Examples

This script looks up an image in an element with the ID #calloutbox and sets its alternative text to a value from a data field.

var altText = record.fields.FavHobby;
query("#callout img").attr('alt', altText);

The following script sets the background color of a specific table cell in an email to red if the value of the field TOTAL has a negative value in the current record.

if(record.fields.TOTAL<0) { 
query("#total").attr("bgcolor","red");
}