Kotlin javascript dataset

0

I have an HTML button with a data attribute:

<button data-shop-listing='{"name": "Towel", "description": "Sunny days and warm weather - you clearly need it after a refreshing jump into the cool sea.", "price": 10.00}'>Add to cart</button>

I want to access the data when the button is clicked using Kotlin. When the click event is triggered, I do this, where item is the HTML element that triggered the event.

fun addItemToCart (item:EventTarget?) {
  console.log(item.dataset.shopListing)
}

However the compiler gives this error: "unresolved reference dataset".

These are my imports:

import org.w3c.dom.*
import org.w3c.dom.events.*
import kotlin.browser.*
    
asked by eloo 27.02.2017 в 12:31
source

1 answer

-1

I respond to myself:

fun addItemToCart (item:EventTarget?) {
val it = item as HTMLElement
  console.log(it.dataset.get("shopListing"))
}

The attribute data set is a method of the HTMLElement type, so you have to transform the EventTarget type to this type. This done, you have to access the native methods of the DOMStringMap, which is the one used by the dataset link

    
answered by 27.02.2017 в 13:11