Como adicionar atributo a um elemento usando jquery?

Hello I am new to this and I want to add the attribute {[[0]} to all the img elements inside div#gallery-1. Como fazer isso usando jQuery?
Tentei todas as respostas, mas recebi o erro "[[3]}". Eu não sou um desenvolvedor da web mas eu quero saber sobre isso. Obrigado.

 0
Author: binary10, 2016-07-19

3 answers

 $("#gallery-1").find('img').each(function() {
   $(this).attr('data-gallery', '');
 })
 2
Author: Kilmazing, 2016-07-19 17:56:54

Não é necessário utilizar each ou find.

// set
$("#gallery-1 img").attr("data-gallery", "value");

// get
$("#gallery-1 img").attr("data-gallery");

Até podes usar data. Isso não é um atributo visível em seu código, é armazenado em um objeto jQuery, mas tem o mesmo efeito.

// set
$("#gallery-1 img").data("gallery", "value");

// get
$("#gallery-1 img").data("gallery");
 2
Author: eisbehr, 2016-07-19 18:13:14
$("#gallery-1").find("img").attr("data-gallery", "");
 1
Author: underscore, 2016-07-19 17:45:33