72

This works:

$(myObj).attr("data-test-1", num1);
$(myObj).attr("data-test-2", num2);

But this doesn't:

$(myObj).attr({
  data-test-1: num1,
  data-test-2: num2
});

Am I missing something really obvious here?

2
  • 1
    Your second example produces a syntax error.
    – Matt
    Commented May 7, 2013 at 15:19
  • 5
    You cannot use dashes in object keys unless you delimit them as a string like {"data-test-1" : "data"}
    – m90
    Commented May 7, 2013 at 15:20

3 Answers 3

151

Sure, like this:

$(myObj).attr({"data-test-1": num1, "data-test-2": num2});

Like the .attr() docs state:

Setting several attributes at once

To change the alt attribute and add the title attribute at the same time, pass both sets of names and values into the method at once using a plain JavaScript object. Each key-value pair in the object adds or modifies an attribute:

$('#greatphoto').attr({
  alt: 'Beijing Brush Seller',
  title: 'photo by Kelly Clark'
});

When setting multiple attributes, the quotes around attribute names are optional.

7
  • 11
    "the quotes around attribute names are optional" only applies to camelCased properties, the OP wants to use dashes, quotes should be mandatory in this case -> Unexpected token -
    – m90
    Commented May 7, 2013 at 15:24
  • @m90 - That's taken directly from the jQuery API, it's not my comment.
    – j08691
    Commented May 7, 2013 at 15:25
  • 4
    I'm aware of that but the dashes are what causes the OP's problems so I guess one should add that.
    – m90
    Commented May 7, 2013 at 15:26
  • Since num1 and num2 are integers, do I need to pass them as num1.toString() as well? Commented May 7, 2013 at 16:05
  • I just tested that and it won't matter as the even num1.toString() still is stored as type Number.
    – j08691
    Commented May 7, 2013 at 16:11
7

Yes it is possible to setup multiple attributes, just use simple Object literal syntax. Example:

$('#my_image').attr({
  alt: 'Beijing Brush Seller',
  title: 'photo by Kelly Clark'
});

More info about attr method can be found here.

1
4

Sorry for posting an answer to an already solved question after so many years.

I just thought about keeping the thread up to date with recommended solution[citation needed] as on date.

Since jQuery 1.2.3, there is a .data() function that takes arguments to get/set data attributes (setting multiple was available since 1.4.3) like this:

/* 
 ** In all the example below, note that 
 ** 'data-' is removed from attribute name
 */

// Setter (single)
$('#my_image').data('test-1', 'num1');
$('#my_image').data('test-2', 'num2');

// Setter (multiple)
$('#my_image').data({'test-1':'num1', 'test-2':'num2'});

// Getter (single)
$('#my_image').data('test-1');    // num1
$('#my_image').data('test-2');    // num2

It must be noted that setting data attributes with .data() doesn't update the DOM, so you can't see these in the DOM inspector. Also, they are not cross compatible with .attr(). However, the data attributes set with .attr() can be retrieved with .data() (in short, any attributes that begin with 'data-' can be retrieved with .data()).

// Setter
$('#my_image').attr('data-test-3', 'num3');    // note the 'data-' on attr name

// Getter
$('#my_image').data('test-3');    // num3
1
  • $('[data-id="5e4deced-0779-4dcd-ae23-d62a51817c1a"]').attr('data-ordinal', 5); $('[data-id="5e4deced-0779-4dcd-ae23-d62a51817c1a"]').data('ordinal'); // Output -- 4 The attr() set does not update data(). So we need to update that separately. Commented Dec 16, 2022 at 7:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.