简体   繁体   中英

Find dynamically added data attribute via .find() not working

I append a value to my HTML body via the .append() function. Later on I need to retrieve this value but I cannot because it is dynamically added. I searched for the cause and found out that I have to use .find() . But even with this method I'm not able to grab the string. Do you eventually see my mistake?

$(document).ready(function(){

    // data[0] = "Tesla X"
    $("body").append('<div id="myCar" data-car="' + data[0] + '"></div>');

    $(document).on('click', '#save', function(e){  
        e.preventDefault(); 

        var page0    = $(this).data('car');  
        var page1    = $('body').find('[data-car]');
        var page2    = $('myCar').find('[data-car]');  
        var page3    = $(document.getElementById('[data-car]')); 
        var page4    = $('#body').find('.bottom').data('car');
        var page5    = $('body').find('.bottom').data('car');
        var page6    = $('body').find('[data-car]');
        var page7    = $('body').find('#myCar').data('data-car');
        console.log(page0);  // not working -> undefined 
        console.log(page1);  // not working -> Object { 0: <div#myCar>, length: 1, prevObject: Object } 
        console.log(page2);  // not working -> Object { length: 0, prevObject: Object } 
        console.log(page3);  // not working -> Object {  } 
        console.log(page4);  // not working -> undefined 
        console.log(page5);  // not working -> undefined 
        console.log(page6);  // not working -> Object { 0: <div#myCar>, length: 1, prevObject: Object } 
        console.log(page7);  // not working -> undefined 
    });

});

see why its not working from the snippet comment

$(document).ready(function(){

    // data[0] = "Tesla X"
    $("body").append('<div id="myCar" data-car="' + data[0] + '"></div>');

    $(document).on('click', '#save', function(e){  
        e.preventDefault(); 

        var page0    = $(this).data('car');  //invalid attribute its target the clicked element not data-car element
        var page1    = $('body').find('[data-car]');//valid one.you are get the element object only
        var page2    = $('myCar').find('[data-car]');  //invalid one missing id mention from beginig `#`
        var page3    = $(document.getElementById('[data-car]')); //invalid jquery object.you are confusing jquery object and js 
        var page4    = $('#body').find('.bottom').data('car');//body its tag not a id name
        var page5    = $('body').find('.bottom').data('car'); //bottom class not in a html
        var page6    = $('body').find('[data-car]'); //its same like page2
        var page7    = $('body').find('#myCar').data('data-car'); //already you are calling the data() function.so its get invalid attribute value .its searching like `data-data-car`

      var page8=$('body').find('[data-car]').data('car')//correct one

      console.log(page0);  // not working -> undefined 
        console.log(page1);  // not working -> Object { 0: <div#myCar>, length: 1, prevObject: Object } 
        console.log(page2);  // not working -> Object { length: 0, prevObject: Object } 
        console.log(page3);  // not working -> Object {  } 
        console.log(page4);  // not working -> undefined 
        console.log(page5);  // not working -> undefined 
        console.log(page6);  // not working -> Object { 0: <div#myCar>, length: 1, prevObject: Object } 
        console.log(page7);  // not working -> undefined 
    });

});

try to use:

$(document).ready(function(){

     var data = "Tesla X"
     $("body").append('<div id="myCar" data-car="' + data + '"></div>');

     var page0   = $(document).find('#myCar');
     console.log(page0);


});

To retrieve the element :

var elem = $('#myCar')

To retrieve its attribute :

$('#myCar').attr("data-car") 

Finally :

var value = $('#myCar').attr("data-car") //Value == Tesla X

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