0
Good community, I have the following script that takes text from the textarea and places it inside a div after ENTER.
$(document).ready(function(){
$('textarea').keypress(
function(e){
if (e.keyCode == 13) {
var msg = $(this).val();
$(this).val('');
if(msg!='')
$('<div class="msg_b">'+msg+'</div>').insertBefore('.msg_push');
$('.msg_body').scrollTop($('.msg_body')[0].scrollHeight);
}
});
If the div "msg_b" is written in HTML in the body the script works, but in this case I specify I need the div "msg_b" to be a js element and this way I can’t get ENTER to send the text entered in the textarea to the div.
var element = '<div class="popup-box chat-popup" id="'+ id +'">';
element = element + '<div class="popup-head">';
element = element + '<div class="popup-head-left">'+ username +'</div>';
element = element + '<div class="popup-head-right"><a href="javascript:close_popup(\''+ id +'\');">✕</a></div>';
element = element + '<div style="clear: both"></div></div><div class="popup-messages"><div class="msg_body"><div class="msg_a">This is from A </div><div class="msg_b">This is from B</div><div class="msg_push"></div></div></div><div class="msg_footer"><textarea class="msg_input" rows="4"></textarea></div></div>';
document.getElementsByTagName("body")[0].innerHTML = document.getElementsByTagName("body")[0].innerHTML + element;
If these div’s come out of this element and stay in the HTML itself The textarea text message already goes into the div "msg_b".
<div class="msg_box" style="right:290px">
<div class="msg_head">User
<div class="close">x</div>
</div>
<div class="msg_wrap">
<div class="msg_body">
<div class="msg_a">This is from A </div>
<div class="msg_b">This is from B, and its amazingly kool nah... i know it even i liked it :)</div>
<div class="msg_push"></div>
</div>
<div class="msg_footer"><textarea class="msg_input" rows="4"></textarea></div>
What has this variable
element
? is an element or an HTML string?– Sergio
@Sergio is an HTML element. Text should appear inside the <div class="msg_b"></div>
– David Concha
I’m still not sure if
element
is an object or a string. You can add more code to the question to clarify it?– Sergio
@Did Sergio notice? i’m not very good at developing javascript code yet, but in this case I need the set of Divs to go through javascript
– David Concha