Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've added a prompt box but can't figure out on how to add the username before the text
eg :- ria:hi
can someone help me with a proper code please?

What I have tried:

HTML
<div class="container-fluid">
    <div class="row">
        <ul id="messages"></ul>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="input-group">
                <span class="input-group-btn">
                    Send! 
                </span>
            </div>
        </div>
    </div>
</div>
JavaScript
const room = 'javascript';
const socket = io.connect('/tech');

$('form').submit(()=>{
    let msg= $('#m').val();
    socket.emit('message', {msg, room});
    $('#m').val('');
    return false;
});

socket.on('connect', ()=>{
    socket.emit('join', {room: room});
});

socket.on('message', (msg) =>{
    $('#messages').append($('<li>').text(msg));
});

var username = prompt('');
Posted
Updated 4-Mar-20 5:04am
v2
Comments
ZurdoDev 4-Mar-20 11:15am    
Where are you stuck?
[no name] 4-Mar-20 14:32pm    
I've used var username = prompt('Enter your username') to enter the input just not aware how to join the input(username)+message
this is my index.js code : tech.on('connection', (socket) => {

socket.on('join', (data) =>{
socket.join(data.room);
tech.in(data.room).emit('message', `New user joined ${data.room} room!`);
});

console.log('User Connected');

socket.on('message',(data) => {
console.log(`message: ${data.msg}`);
tech.in(data.room).emit('message', data.msg);
});
and the js.html code is as follows: const room = 'javascript';
const socket = io.connect('/tech');

$('form').submit(()=>{
let msg= $('#m').val();
socket.emit('message', {msg, room});
$('#m').val('');
return false;
});

socket.on('connect', ()=>{
socket.emit('join', {room: room});
socket.on('message', (msg) =>{
$('#messages').append($('').text(msg));
});

var username = prompt('Enter your username')
ZurdoDev 4-Mar-20 14:56pm    
" not aware how to join the input(username)+message" - What does that mean? Because you join together two strings all you do is
var1 + var2;

1 solution

You're using jquery or some other framework - not how you noted your question, but,

Here, in real javaScript, is a method:

For this example:
First, let the user name be in the variable uid
Let your message be in variable msg

Then the script would display the message as
JavaScript
alert(uid + ': ' + msg);
However - since you couldn't work it out yourself and yet you had code with sophistication at an arguably higher level than appending two strings in javaScript I'd say you just copied the code from somewhere and are quite clueless. I've given you the pieces, but you'll need to assemble them yourself inside your code - trivial if I'm wrong about you.
 
Share this answer
 
Comments
Richard MacCutchan 4-Mar-20 11:18am    
"you just copied the code from somewhere"
Doesn't everyone?
[no name] 4-Mar-20 14:29pm    
right? I looked up some yt videos and this is my first project(which i had to develop) so i think i'm doing okay until i understand what i'm doing :) I've used var username = prompt('Enter your username') to enter the input just not aware how to join the input(username)+message
this is my index.js code : tech.on('connection', (socket) => {

socket.on('join', (data) =>{
socket.join(data.room);
tech.in(data.room).emit('message', `New user joined ${data.room} room!`);
});

console.log('User Connected');

socket.on('message',(data) => {
console.log(`message: ${data.msg}`);
tech.in(data.room).emit('message', data.msg);
});
and the js.html code is as follows: const room = 'javascript';
const socket = io.connect('/tech');

$('form').submit(()=>{
let msg= $('#m').val();
socket.emit('message', {msg, room});
$('#m').val('');
return false;
});

socket.on('connect', ()=>{
socket.emit('join', {room: room});
socket.on('message', (msg) =>{
$('#messages').append($('').text(msg));
});

var username = prompt('Enter your username')
W Balboos, GHB 5-Mar-20 7:53am    
Tell me - would you use a youTube video before doing your own dental work? How about fixing an automobile transmission?

Go here: https://www.w3schools.com/js/default.asp and learn the language - it's done in a very relaxed manner with the ability for you to try things out. You didn't even know what language variant you were using in your topic selection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900