Click here to Skip to main content
15,122,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to implement chatting features in a website. I used here prompt to get client name. Its working properly but problem is that "prompt" shows popup on every page load. I want to get client name by textbox not by the prompt. What should I do?

I am trying this code

What I have tried:

<div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" /><br />
        <button onclick="myFunction()">Try it</button>
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div><script type="text/javascript">
       function myFunction() {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.
                $('#discussion').append('<strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg);
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
Updated 5-Apr-17 0:00am

1 solution

I want to get client name by textbox not by the prompt. What should I do?

remove this line
$('#displayname').val(prompt('Enter your name:', ''));

I assume #displayname is the textbox since you have used .val() function

Note: ensure that empty data is not passed the hub, you need to validate it.

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