Solution 1 :

dataset is the entire data set, i.e. the result of all data-* attributes on the element or properties set via the element data API.

You need to reach into it and get the property you need. In short, change

var homeC = navI.dataset


var homeC = navI.dataset.home

Also be aware that these days var is not recommended; use let or const.


Since you know say the home element has the ID “home”, not the class “home”, you need:


Problem :

I’m new to javascript And facing a little issue,
I hope I will get the solution here

I have a nav Item with a data like this

<button data-home="home" class="nav-item"></button>

I did select the button element as a var and got its dataset

var navI = document.querySelector('.nav-item')

var homeC = navI.dataset

And then I wanted to set this item as a class to select a new element

new element,

<div class="home"></div>

And placed the dataset like this to select the element

var pageHome = document.querySelecter('.' + homeC)

But pageHome comes with a null element

Pls pass me a solution if possible


Comment posted by YS Nirob

I used it but using querySelector is giving a null element

Comment posted by Mitya

Then the element doesn’t exist at the time the code runs. Try to reproduce the issue in a Fiddle.

Comment posted by YS Nirob

If I use id = “home” instead of class and write it like this

Comment posted by Mitya

Huh? You said in your question the home element had the

Comment posted by YS Nirob

I just said if I use Id instead of class it works but I need to make it work with class . I mean querySelector not getElementById.Kindly if you know how to make it work with querySelectors pls let me know