user9231701
user9231701

Reputation:

JS addEventListener -> update data-action

I have a game where you press a button, depending on what you press it shows up a menu by using data-action. E.g I got a main menu, press "news" and the news menu will be called using data-action="news".

Now I will return data via the game, and will therefore send that data (not relevant) over via JS using addEventListener. However, I'd like to make an if-statement which will set the data-action to whatever was sent from the game. How to update data-action via JS?

If event.date.type is "news" -> show the new-news menu which will be shown by setting data-action to "new-news". This doesn't work, how to do it?

if (event.data.type == "news"){
    data-action="new-news"
}

Upvotes: 0

Views: 470

Answers (2)

SirPedr
SirPedr

Reputation: 127

From what I understand, you are having trouble assigning a data element to some element. It seems to be just a syntax error.

Instead of:

data-action = "new-news"

Use the dataset like following:

element.dataset.action = "new-news"

This should work :)

Upvotes: 1

mmlr
mmlr

Reputation: 2165

Use the dataset interface:

element.dataset.action = 'new-news'

Upvotes: 1

Related Questions