Skip to main content
Use the dot notation to access the property. Bracket notation should be used only with variables.
Source Link
Pierre Spring
  • 10.6k
  • 13
  • 50
  • 44

In vanilla-JS, assuming spanEl is a reference to the DOM node

spanEl.dataset['complexKey'] dataset.complexKey

will work using the camelCasecamelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-) and also

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed. As a side note, in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

In vanilla-JS, assuming spanEl is a reference to the DOM node

spanEl.dataset['complexKey'] 

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-) and also

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed. As a side note, in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

In vanilla-JS, assuming spanEl is a reference to the DOM node

spanEl.dataset.complexKey

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-) and also

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed. As a side note, in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")
deleted 3 characters in body
Source Link
Fabrizio Calderan
  • 122.6k
  • 26
  • 168
  • 181

in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

(assumingIn vanilla-JS, assuming spanEl is a reference to the DOM node). In straight javascript

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed and also

spanEl.dataset['complexKey'] 

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-) and also

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed. As a side note, in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

(assuming spanEl is a reference to the DOM node). In straight javascript

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed and also

spanEl.dataset['complexKey'] 

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-)

In vanilla-JS, assuming spanEl is a reference to the DOM node

spanEl.dataset['complexKey'] 

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-) and also

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed. As a side note, in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")
added 93 characters in body
Source Link
Fabrizio Calderan
  • 122.6k
  • 26
  • 168
  • 181

in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

(assuming spanEl is a reference to the DOM node). In straight javascript

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed and also

spanEl.dataset['complexKey'] 

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-)

in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

(assuming spanEl is a reference to the DOM node). In straight javascript

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed and also

spanEl.dataset['complexKey'] 

using the camelCase notation (see http://jsbin.com/oduguw/3/edit)

in jQuery you can access to that data attribute with

$(spanEl).data("complex-key")

(assuming spanEl is a reference to the DOM node). In straight javascript

spanEl.getAttribute('data-complex-key')

will work fine as you already noticed and also

spanEl.dataset['complexKey'] 

will work using the camelCase notation (see http://jsbin.com/oduguw/3/edit) when your data attribute contains hypens (-)

added 93 characters in body
Source Link
Fabrizio Calderan
  • 122.6k
  • 26
  • 168
  • 181
Loading
Source Link
Fabrizio Calderan
  • 122.6k
  • 26
  • 168
  • 181
Loading