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
to
var homeC = navI.dataset.home
Also be aware that these days var
is not recommended; use let
or const
.
[EDIT]
Since you know say the home element has the ID “home”, not the class “home”, you need:
document.querySelector('#'+homeC);
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
Then the element doesn’t exist at the time the code runs. Try to reproduce the issue in a Fiddle.
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