简体   繁体   中英

jQuery updating elements added on the fly?

My code is not working, i think because elements are added on the fly :

var tooltip = $('<div/>').insertAfter('.trigger').addClass('tooltip');
var tname = $('<span/>').addClass('tname').text('(...)');
tooltip.html(tname.html()):

// Ajax call
success: function() {
  tname.html('success'); // not working
  $('.tooltip').find('.tname').html('success'); // not working
  $('.tname').html('success'); // not working
}

You're not inserting the span into the DOM.

//                                                       append to some element
var tname = $('<span/>').addClass('tname').text('(...)').appendTo(tooltip);

Only then you can use selectors to find the element and do something with it.

This won't work because you are not inserting the tname element into the DOM. See this fix below:

var tooltip = $('<div/>').insertAfter('.trigger').addClass('tooltip');
var tname = $('<span/>').addClass('tname').text('(...)');
tooltip.html("");
tooltip.append(tname);

// Ajax call
success: function() {
  tname.html('success'); // should work
  $('.tooltip').find('.tname').html('success'); // should work
  $('.tname').html('success'); // should work
}

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