I have a navigation which is listed dynamically with PHP.
<nav id='navs'> @foreach($element as $val) <a data-prop={{$val->id}}>$val->name</a> @endforeach </nav>
The html : The highlighted the tags are listed dynamically
I have a JS script, when we click on the navbar element then we add .selected class to the tag. When the page is loaded the first tag has .selected class.
I need a JS code to find the data-prop attribute value for the elements with .selected class. I tried:
var d= $("#navs .selected").attr('data-prop');
but the d variable is undefined. Any suggestion? I also tried the .find() method.
Advertisement
Answer
I figured out what the problem was. I have a script which is adding the class name .selected to the tags, the problem is that it executed AFTER my script, where I wanted to find the element with the .selected class.
So my code is working:
var d= $("#navs .selected").attr('data-prop');