javascript - Cant find dynamically created ids with jquery -


i create ids following:

var plant_counter = 0;  $('.plant-name').attr('id', 'plant-name-' + plant_counter); 

after try change html code of element:

    $('#nav-btn-test').click (function () {     $('#plant-name-0').html('hello');     console.log('clicked); }); 

the html code not change. why?

the code setting ids not work because variable plant_counter not being updated (it 0).

use jquery's .each method (see documentation here), this:

$('.plant-name').each(function( index ) {     $(this).attr('id', 'plant-name-' + index); }); 

aside: in click handle registration, you're missing closing single quote (') @ end of logging statement.

here working example jsfiddle copy.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -