Hi guys I'm trying to get the ids of the <li>
but im getting undefined, I'm using laravel.
This is in my view
<ul class="nav nav-pills nav-stacked" id="listas">
@foreach(Auth::user()->empresas as $empresa)
<li>
<a href="#" class="seleccion" id="{{$empresa->id}}" data-id="{{$empresa->id}}"
data-nombre="{{$empresa->nombre}}">{{$empresa->nombre}}
<span class="pull-right text-red item{{$empresa->id}}"></span></a>
</li>
@endforeach
</ul>
My js
$(document).on('click', '.seleccion', function () {
$('#listas').find('li').each(function () {
console.log($(this).attr('id'))
})
})
Since your li
s don't actually have an id
attribute, you should look for it in the a
child of your li
s, like this.
$(document).on('click', '.seleccion', function () {
$('#listas').find('li > a').each(function () {
console.log($(this).attr('id'));
});
});
You try to read a not existing attribute from your <li>
elements.
@foreach(Auth::user()->empresas as $empresa)
<li id="myId"> <--- HERE
<a href="#" class="seleccion" id="{{$empresa->id}}" data-id="{{$empresa->id}}"
data-nombre="{{$empresa->nombre}}">{{$empresa->nombre}}
<span class="pull-right text-red item{{$empresa->id}}"></span>
</a>
</li>
@endforeach
greetings
Like the li don't has id you must change your markup or change the js
$(document).on('click', 'a.seleccion', function () {
console.log($(this).attr('id'));
});
You can use $(this)
$( ".seleccion" ).click(function() {
var id = $(this).attr('data-id'));
console.log(id);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.