-2

jquery on click get attr

<div class="benzer">
      <i class="off" pk="125">click me</i>
    </div>
<script>
 $(document).ready(function(){
    $(".benzer").on("click",".off",function(){
        $pk = $(this).attr("pk");   
        console.log($pk);
    });
)};
<script>

"pk" get value undefined ??

help me please, jquery on properties for div get attr not working?

https://jsfiddle.net/ktb5ky46/1/

2 Answers2

0

There no attribute as pkand $pk is not a variable. )};<script> should be })</script> html

<div class="benzer">
      <i class="off" pk="125">click me</i>
    </div>

js

 $(document).ready(function(){
    $(".benzer").on("click",".off",function(){
        pk = $(this).attr("pk");   
        console.log(pk);
    });
});

edit ok now u added attr pk. now get the fiddle here

edit jsfiddle

Rasel
  • 4,555
  • 3
  • 26
  • 35
0

Hi just use data attribute to avoid undefined pk attribute

<div class="benzer">
      <i class="off" data-pk="125">click me</i>
    </div>


  $(document).ready(function(){
    $(".off").click(function(){
   console.log($(this).attr('data-pk'));//125
    });
});

http://jsfiddle.net/divakarvenu/j484e6hh/1/

divakar
  • 1,339
  • 4
  • 12
  • 29