Good time of day!
I need to add elements to the page, and then work with their fields. When adding (append, html) I give them data.
The problem is that when an event changes the fields of an element, I cannot access the date of the parent. On change, bind do not catch elements at all. I only get to catch the event with this construct:
$(document).on({ change: function() { } }, '.el'); But when trying to contact a parent
$(this).parent().parent().parent().parent().data("material-el-id") or
$(this).parent().parent().parent().parent().data() or
$(this).parent('.parentClass').data() Only undefined flies to me.
How to get data from the parent of the created item?
Original:
$('.add-material-btn').click(function () { loop1: for (i = 1; i <=5; i++){ var type = getFromData('material-' + i + '-type'); if (type == '' || type == null){ $(".add-material-block-past").append('<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 napolnenie-el" data-material-el-"' + i + '">'+$(".napolnenie-el-set" ).html()+'</div>'); setDataAndText('material-' + i + '-type', 0); LAST_EDD_NAPOLNENIE_ID = i; break loop1; } } }); $(document).on({ change: function() { console.log(($(this).parent().parent().parent().parent().data("material-el-id"))); } }, '.napolnenie-el-material');