I'm trying to append the next div after the chatUserSend()
function, but when I put the new append after the said function, the div displayed already even the chatUserSend()
has not yet been clicked
How to fix this?
Please see the updated JS script.
var custLoanAccountNum = '<span id="custLoanAccountNum" class="chat_msg_item chat_msg_item_admin"><p>What is your Loan Account Number?</p></span>';
var custName = '<span id="custConcern" class="chat_msg_item chat_msg_item_admin"><p>Type your concern or message to Customer Support</p></span>';
$("#concernType").on('click', 'li', function(){
var concernType = $(this).html();
$('.chat_converse').append("<span class='chat_msg_item chat_msg_item_user'><p>" + concernType + "</p></span>");
if($(this).attr('id')){
switch ($(this).attr('id')){
case 'concernTypeCol':
$('.chat_converse').append("<span id='custTypelabel' class='chat_msg_item chat_msg_item_admin'><p>Are you a new or existing customer?</p></span><span id='custType' class='custTypelist chat_msg_item'><ul id='custType'><li id='newCustType'>New Customer</li><li id='existingCustType'>Existing Customer</li></ul></span>");
$("#custType").on('click', 'li', function(){
var custType = $(this).html();
$('.chat_converse').append("<span class='chat_msg_item chat_msg_item_user'>" + custType + "</span>");
if($(this).attr('id') == 'newCustType'){
$('.chat_converse').append(custName);
chatUserSend();
} else if ($(this).attr('id') == 'existingCustType'){
$('.chat_converse').append(custLoanAccountNum);
chatUserSend();
$('.chat_converse').append(custName);
}
});
break;
}
}
});
function chatUserSend(){
$('#fab_send').click(function(event){
var thought = $('#chatSend').val();
$('.chat_converse').append("<span class='chat_msg_item chat_msg_item_user'>" + thought + "</span>");
//sendMessage();
$('#chatSend').val('');
return false;
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chat_converse" class="chat_conversion chat_converse">
<span id="concernTypelabel" class="chat_msg_item chat_msg_item_admin">
<p>Hello, how can we help you? Please select one?</p></span>
<span id="concernType" class="chat_msg_item ">
<ul id="concernType">
<li id="concernTypeCol">I need assistance in paying my loan. (Collections)</li>
<li id="concernTypeCS">I want to talk to a customer service representative. (Customer Service)</li>
<li id="concernTypeHR">I want to explore job opportunities. (Human Resource)</li>
</ul>
</span>
</div>
<div class="fab_field">
<a id="fab_send" class="fab">Send</a>
<textarea id="chatSend" name="chat_message" placeholder="Send a message" class="chat_field chat_message"></textarea>
</div>