简体   繁体   中英

How to alert the data-id attribute value in laravel?

Ajax1:

<script type="text/javascript">
$(document).ready(function () {
    $('.CategoryName').click(function() {
        var categoryName = $(this).attr("data-id"); 
        var url="{{url('getSubCategoryName/').'/'}}" + categoryName;
        $.ajax({
            url : url,
            type: 'GET',
            dataType:'json',
            success:function(data){
                $.each(data, function( index, value ) {
                    var output = '';
                    output = ' ' + value.subCategoryName + '';
                    **$(".subcategory").append('<ul><li data-
                    id="'+value.subCategoryName+'" class="getDeviceCategoryName">'+value.subCategoryName+'</li></ul>')**
                });
            }
        });
    });
});  
</script>

The code which is marked in bold has the value for data-id attribute..And I need to pass that value when class named getDeviceCategoryName is clicked.

AJAX2:

<script type="text/javascript">
$(document).ready(function () {
    $('.getDeviceCategoryName').click(function() {
        var subCategoryName = $(this).attr("data-id"); 
        alert(subCategoryName); 
    });
});  
</script>

This is one I tried for that..But its not working.It doesnot alerts or shows any error.

$(document).ready(function () {
        $(document).on('click', '.getDeviceCategoryName', function(){
        var subCategoryName = $(this).attr("data-id"); 
        alert(subCategoryName); 
    });
});  

You should consider jquery event delegation: Change your jquery selector line like this:

<script type="text/javascript">
$(document).ready(function () {
    $(document).on('click','.getDeviceCategoryName',function() {
        var subCategoryName = $(this).attr("data-id"); 
        alert(subCategoryName); 
    });
});  
</script>

Here you go with a solution

$(document).ready(function () {
  $(document).on('click', 'li.getDeviceCategoryName', function() {
    console.log($(this).data("id")); 
  });
});  

Since the elements are generated dynamically, so you need to delegate events .

For receiving the data-attribute you can do $(this).data("attribute-name"); in your scenario it will be $(this).data('id');

Hope this will help you.

Personally I think code is more efficient if the click event is applied to the element right after it's created.

$(function () {
  $('.CategoryName').click(function () {
    var categoryName = $(this).attr('data-id');
    var url = '{{url(\'getSubCategoryName/\').\'/\'}}' + categoryName;
    $.ajax({
      url: url,
      type: 'GET',
      dataType: 'json',
      success: function (data) {
        $.each(data, function (index, value) {
          var output = '';
          output = ' ' + value.subCategoryName + '';
          $('.subcategory').append($('<ul/>').append('<li data-id="' + value.subCategoryName + '" class="getDeviceCategoryName">' + value.subCategoryName + '</li>')
                                   .find('li')
                                   .click(function () {
                                    var subCategoryName = $(this).attr('data-id');
                                    alert(subCategoryName);
          });
          );
        });
      }
    });
  });
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM