Ajay Ajay - 11 days ago 7
jQuery Question

find value of id in jquery

when I am trying to fetch the value of id, it returns me 0; I want "a".
Thanks in ADVANCE

<script>
$(document).ready(function() {
$('#lettera').click(function(){
var a = $('#lettera').val();console.log(a);
});
});
</script>
<section>
<div class="container">
<div class="row">
<ul class="list-inline">
<?php
foreach($alphas as $letter){
echo ($this->Html->tag('li', $letter, ['id' => 'letter'.$letter]));
}
?>
</ul>
</div>
</div>
</section>

Answer

The element you're creating appears to be an li, and li don't have a value attribute. Presumably you're looking for the text of the element, so use text() instead:

$('#lettera').click(function(){
  var a = $(this).text();
  console.log(a);
});