omrehman omrehman - 1 month ago 7
jQuery Question

Jquery : how to get name of next element



I had written a jquery code


var next_element =$(this).next();
alert("name="+next_element);


In a jquery event. But I got Ouput as
name=undifined
. why did it happens? if this is incorrect, how did i get the name of element next to the event happening ??

Answer

$('#qew').click(function() {

  alert($(this).next().attr('name'))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="qew">this</button>
<button name="that">that</button>

Comments