Foros de discusión

How to put the attribute "data-userId"

l ll, modificado hace 9 años.

How to put the attribute "data-userId"

Regular Member Mensajes: 158 Fecha de incorporación: 12/12/11 Mensajes recientes
Hi!

var el = A.Node.create('<div></div>');
el.set('id',userId);
el.set('data-userId',userId);
el.setData('data-userId',userId);


3 and 4 lines not work
thumbnail
Kyle Joseph Stiemann, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Liferay Master Mensajes: 760 Fecha de incorporación: 14/01/13 Mensajes recientes
Hi,
Try using element.setAttribute() instead of Node.set():

el.setAttribute('data-userId',userId);

- Kyle
l ll, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Regular Member Mensajes: 158 Fecha de incorporación: 12/12/11 Mensajes recientes
I can not mix aui and pure js.
A.Node.create('<div data-userid="</code></pre><br /><img alt=" emoticon" src="@theme_images_path@/emoticons/smile.gif"></div>
thumbnail
Evan Thibodeau, modificado hace 9 años.

RE: How to put the attribute "data-userId"

New Member Mensajes: 14 Fecha de incorporación: 29/03/12 Mensajes recientes
Instead of using ".set" try using ".attr" like this:

el.attr('data-userId', userId);
thumbnail
Kyle Joseph Stiemann, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Liferay Master Mensajes: 760 Fecha de incorporación: 14/01/13 Mensajes recientes
l ll:
I can not mix aui and pure js.
A.Node.create('<div data-userid="</code></pre><br /><img alt=" emoticon" src="@theme_images_path@/emoticons/smile.gif"></div>
<br>Why not?<br><br><a href="http://jsfiddle.net/qypt9tcx/1/">This code works perfectly for me by the way:</a><br><br><span style="font-family: Courier New">var el = A.Node.create('&lt;div&gt;&lt;/div&gt;');<br>el.set('id',userId);<br>el.setAttribute('data-userId',userId);</span><br><br>- Kyle
thumbnail
David H Nebinger, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
Wait, isn't the setData() and getData() methods supposed to be preferred over direct attribute access? The whole point was to treat the attribs as keyed data and using the data methods enforces this pattern.

So in that case the OP should use el.getData("userId") and el.setData("userId", userId).
l ll, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Regular Member Mensajes: 158 Fecha de incorporación: 12/12/11 Mensajes recientes
Kyle Joseph Stiemann

Excuse me. It appeared that I use AUI 1.5 and there is no setAttribute there. At the same time you showed an instance of AUI 2.0. That is why we did not understand each other.
thumbnail
Kyle Joseph Stiemann, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Liferay Master Mensajes: 760 Fecha de incorporación: 14/01/13 Mensajes recientes
When I test the code with AlloyUI 1.5 it also works. Perhaps there's a js error in your code.

@David,
The docs for AlloyUI 1.5 recommend Node.setAttribute() for custom attrs, and specify that Node.setData() will not affect the DOM node. I don't know if that changed in other versions though.

- Kyle
thumbnail
David H Nebinger, modificado hace 9 años.

RE: How to put the attribute "data-userId"

Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
@Kyle: Nuts

emoticon

Actually I had gotten there from a different path, was working through the source for the aui:nav-item tag where the getData() is used vs getAttribute() and online stuff saying it's preferred because it clearly signifies intents...

Ah, well, thanks for following up.
l ll, modificado hace 8 años.

RE: How to put the attribute "data-userId" (Respuesta)

Regular Member Mensajes: 158 Fecha de incorporación: 12/12/11 Mensajes recientes
Thanks for the answer.

Will be correct:


getData('userId');
and
setData('userId',1111);