0
$newUser.addClass(newTweet.user).attr('data-user', newTweet.user).text('@' + newTweet.user + ': ');
var user = $(this).data('user');

On line 1 newTweet is an object, but I don't understand the parameter 'data-user. This isn't referenced in any of the other files, user, so is the 'data-' prefix performing some action? Or does it somehow reference line 2? Or am I completely wrong in both situations, I also don't understand how .addClass() method is working in this situation. How does a property of an object get added as a class?

Marcus
  • 53
  • 1
  • 8
  • 2
    Possible duplicate of [jQuery Data vs Attr?](https://stackoverflow.com/questions/7261619/jquery-data-vs-attr). and for addClass is not a property of an object is the value of that property . `newTweet = { user: 'some-user'; }` so the string `some-user` is added as a class to a HTML element – Mihai T Apr 01 '19 at 11:00

1 Answers1

2

The .attr() function adds an attribute to an element. You can read more about it here: http://api.jquery.com/attr/.

data-user is the name of the attribute added to the element. It doesn't provide any actions, but it's used to store data.

The data-* attributes is used to store custom data private to the page or application.

The data-* attributes gives us the ability to embed custom data attributes on all HTML elements.

The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).

The data-* attributes consist of two parts:

The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix "data-" The attribute value can be any string

Note: Custom attributes prefixed with "data-" will be completely ignored by the user agent.

https://www.w3schools.com/tags/att_global_data.asp.

Both for the .attr() call and .addClass() it uses the value of newTweet.user, which might be a string.

SebastiaanYN
  • 546
  • 6
  • 12
  • Oh I forget to specify, `newTweet.user` is an array. So it's adding it's value to `$newTweet`? – Marcus Apr 01 '19 at 11:23
  • 1
    It stores the `newTweet.user` array in the element `$newUser`. You can access it by using `$newUser.attr('data-user')` which would give back the array. It stores it as an object, so you don't need to do any extra work. – SebastiaanYN Apr 01 '19 at 11:26
  • Then what does `.addClass(newTweet.user)` add to `$newTweet`? – Marcus Apr 01 '19 at 11:29
  • 1
    Calling `.addClass()` with an array doesn't do anything. It only accepts strings, as seen in the docs: https://api.jquery.com/addclass/. – SebastiaanYN Apr 01 '19 at 11:33
  • So I could delete `.addClass()` and it wouldnt change anything? – Marcus Apr 01 '19 at 11:34
  • Huh, that worked, Thanks for the help, wish I could thumb you up more – Marcus Apr 01 '19 at 11:35
  • So just to clarify cause I'm still a little unsure. `$newTweet.attr('data-user', newTweet.user` Stores the value of `newTweet.user` in `newTweet` under the key `'data-user'`? – Marcus Apr 01 '19 at 11:39