I'm learning jQuery, I wrote the following script here: http://jsfiddle.net/hspouwnx/14/ The bottom line is that you need to get the text from a div with the message class, if this div is in the document directly then there is no problem I get this data like this var text = $(".message").text(); but if the data arrived from AJAX or were created dynamically, this method does not select them.

I ask for help and solutions to this problem.

  • Find you can use to find a new element - Serge Esmanovich

2 answers 2

The moment you get the value, the element is not in the DOM, but if you look at the event, everything will be fine. Slightly modified code from your example http://jsfiddle.net/hspouwnx/14/

 $('#shop_msg2').click(function () { var text = $(".message2").text() || "Текста нет"; $.MyStick({ note: text, // или вообще так // note: $(".message2").text(), className: 'message', position: { right: 0, bottom: 0 }, time: 3000, speed: 300 }); }); 

    Assigning the value of the variable text2 occurs when the page loads. And when the page is loaded into .message2 is empty, therefore, an empty value is remembered. And then it does not change, i.e. forever remains empty. And if you move the values ​​to the inside of the function, text2 will receive the value each time you click on the button and when the value appears, text2 will change from empty to text that appears in # shop_msg2: *

     $('#shop_msg2').click(function () { var text2 = $(".message2").text(); $.MyStick({ note: text2, className: 'message', position: { right: 0, bottom: 0 }, time: 3000, speed: 300 }); });