Greetings) Help please)) When an Ajax request dynamically creates an element on the page. How to get the attribute of a dynamically added element when clicking on it? In the console (main.js: 421 Uncaught TypeError: this.attr is not a function)
//Π€ΡΠ½ΠΊΡΠΈΡ ΠΊΠΎΡΠΎΡΠ°Ρ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π² success function moveAjax(answerAjax){ var objAjax = answerAjax; (function addBtn(answerAjax){ let tabBtns = $('.tab-btns'); for(let i = 0; i < objAjax.length; i++){ var btnData = objAjax[i].data; //ΠΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΎΠ΅ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° $(tabBtns).append('<li><span data="' + btnData + '">' + objAjax[i].name + '</span></li>'); console.log(objAjax[i].name); }; })(); console.log(objAjax); (function(){ //ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠΎΠ±ΡΡΠΈΠΉ Π½Π° Π΄ΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Ρ ΡΠ»ΠΊΠΌΠΊΠ½Ρ $(".tab-btns>li").on("click", "span", function(){ console.log(this.attr("data")); //ΠΠΎΠΏΡΡΠΊΠ° ΠΏΠΎΠ»ΡΡΠΈΡΡ Π°ΡΡΠΈΠ±ΡΡ "data" Π΄ΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° }) })(); }; //ΠΠ°ΠΏΡΠΎΡ $.ajax({ dataType: "json", url: 'js/json/prices/prices.json', cache: false, error: function(XHR, status, error) { console.log(status+' '+error); }, success: function(answerAjax) { moveAjax(answerAjax); } });
$(this).attr(...)- jQuery wrapper is needed here.tabBtns.append(- but is not needed here. - Igor