phoenixCoder phoenixCoder - 7 months ago 33
Javascript Question

Trying to code jQuery HTML5 chat box program with mouse click event response

I am having issues coding a chat box program for a school project. I am having 3 problems with the program. First, my chat message does not post properly in my chat box. Secondly, I want to be able to click exit and have it close to window but am not completely sure how to do it. Lastly, I would like the chat to echo the message or to have ahard coded response at the least. I'm researching how to do that I'm not quite sure how to do this. I have a style sheet and html. Here is my html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src=http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js ></script>
<link rel="stylesheet" type="text/css" href="jQuery.css">



//my issues seem to be in my Javascript between here************************************



<script type="text/javascript">
$(document).ready(function(){
$('#submitmsg').click(function(){
var message = $('#usermsg').val()
var old = $('#chatbox').html()

$('#chatbox').html(old + '<p>' + message + '</p>');
});
});
</script>
<script >
function close_window(url){
var newWindow = window.open('', '_self', ''); //open the current window
window.close(url);
};
</script>


//and here*****************************************************************************
</head>
<body>
<div id="wrapper">
<div id="menu">
<p class="welcome">Welcome <b></b></p>

//Possible issue here*******************************************************************
<p class="logout"><a id="exit" href="#" onclick="window.close();">Exit Chat</a></p>
<div style="clear:both"></div>
</div>

<div id="chatbox">
<p>Here's our chat data</p>
</div>

<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
</div>
</body>
</html>


Here is my css:

body {
font:12px arial;
color: #222;
text-align:center;
padding:35px; }

form, p, span {
margin:0;
padding:0; }

input {
font:12px arial; }

a {
color:#0000FF;
text-decoration:none; }

a:hover {
text-decoration:underline; }

#wrapper, #loginform {
margin:0 auto;
padding-bottom:25px;
background:#EBF4FB;
width:75%;
border:1px solid #ACD8F0; }

#loginform {
padding-top:18px; }

#loginform p {
margin: 5px; }

#chatbox {
text-align:left;
margin:0 auto;
margin-bottom:25px;
padding:10px;
background:#fff;
height:270px;
width:430px;
border:1px solid #ACD8F0;
overflow:auto; }

#chatbox p {
padding:1em;
margin:1em;
background:#E6E6E6;
border:1px solid #BDBDBD;
-moz-border-radius:4px;
}

#usermsg {
width:395px;
border:1px solid #ACD8F0; }

#submit {
width: 60px; }


.welcome {
float:left; }

.logout {
float:right; }

.msgln {
margin:0 0 2px 0; }


Some of the css is unnecessary I'm pretty sure but when I tried taking some out it caused issues. Any help is greatly appreciated.

Answer

try copying the following - i put the styling into the head, moved the javascript to the end, and swapped the input for a button. This is ties to the onclick and appends the new comment into the chat window. It works, but could be done better, also I have not addressed the issue of closing the window, but this does post the new comment into the chat region.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" ></script>
<style>
body {
font:12px arial;
color: #222;
text-align:center;
padding:35px; }

form, p, span {
    margin:0;
    padding:0; }

input { 
    font:12px arial; }

a {
    color:#0000FF;
    text-decoration:none; }

a:hover { 
    text-decoration:underline; }

#wrapper, #loginform {
    margin:0 auto;
    padding-bottom:25px;
    background:#EBF4FB;
    width:75%;
    border:1px solid #ACD8F0; }

#loginform { 
    padding-top:18px; }

#loginform p { 
    margin: 5px; }

#chatbox {
    text-align:left;
    margin:0 auto;
    margin-bottom:25px;
    padding:10px;
    background:#fff;
    height:270px;
    width:430px;
    border:1px solid #ACD8F0;
    overflow:auto; }

#chatbox p {
    padding:1em;
    margin:1em;
    background:#E6E6E6;
    border:1px solid #BDBDBD;
    -moz-border-radius:4px;
}

#usermsg {
    width:395px;
    border:1px solid #ACD8F0; }

#submit { 
    width: 60px; }


.welcome { 
    float:left; }

.logout { 
    float:right; }

.msgln { 
    margin:0 0 2px 0; }
    </style>
</head>
<body>
<div id="wrapper">
    <div id="menu">
        <p class="welcome">Welcome</p>
        <p class="logout"><a id="exit" href="#" onclick="window.close();">Exit Chat</a></p>
        <div style="clear:both"></div>
    </div>

    <div id="chatbox">
        <p>Here's our chat data</p>
    </div>

    <form name="message">
        <input name="usermsg" type="text" id="usermsg" size="63" />
        <button type="button" id="submitmsg" value="send">Send</button>    
    </form>
</div> 

<script>
    $(document).ready(function(){
        $('#submitmsg').click(function(){
            var message = $('#usermsg').val();
            $('#chatbox').append('<p>' + message + '</p>');
            $('#usermsg').val('');
        });
    });

 function close_window(url){
        var newWindow = window.open('', '_self', ''); //open the current window
        window.close(url);
    };
</script>

</body>
</html> 
Comments