The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> Using the <input> tag, Two <input type="text"> tags on the same page.
Rayj00
post Jun 1 2020, 12:46 PM
Post #1





Group: Members
Posts: 6
Joined: 1-June 20
Member No.: 27,369




I admit I am not a seasoned website developer, so have mercy on me! smile.gif

So, I have two <input type="text"> on the same page.

Here is the first one:

CODE
<input type="text" id="broadcast-id" value="room-xyz" autocorrect=off autocapitalize=off size=20>
<button id="open-or-join">Open or Join Broadcast</button>

And here is the second one:

CODE
<div>
              <h2>Messages</h2>
              <ul></ul>
      </div>
  <form action="">
    <input class="msgs" type="text" id= 'msgs' />
  <button>Send</button>
  </form>


So the first one works fine. But the second one grabs the text from the first one instead of what is typed in it's own text box.

I am using some sample code to add a messaging option in the page from https://www.ably.io/concepts/socketio

Any ideas why the second input is grabbing the first input text?

Thanks, appreciate your response.

Ray



User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
Christian J
post Jun 1 2020, 06:03 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



The HTML code sample alone won't do anything. Probably there is also some javascript that adds functionality (but apparently in an incorrect way).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Rayj00
post Jun 1 2020, 07:38 PM
Post #3





Group: Members
Posts: 6
Joined: 1-June 20
Member No.: 27,369



QUOTE(Christian J @ Jun 1 2020, 07:03 PM) *

The HTML code sample alone won't do anything. Probably there is also some javascript that adds functionality (but apparently in an incorrect way).


here is all the code that I am having a problem with:

CODE
<div>
              <h2>Messages</h2>
              <ul></ul>
      </div>
  <form action="">
    <input type="text"  />
  <button>Send</button>
  </form>


And java script:

CODE
const form = document.querySelector("form");
const input = document.querySelector("msg");
messageList = document.querySelector("ul");

// handle sending message to server & input reset
        function sendMessage(e) {
                // handle sending message to server & input reset
                e.preventDefault();
                // send input value to server as type 'message'
                socket.emit("message", input.value);
                console.log("message = ", input.value);
                // reset input value
                input.value = "";
        }

                // add listener to form submission
                form.addEventListener("submit", sendMessage);

// add message to our page
        function addMessageToHTML(message) {
                // create a new li element
                const li = document.createElement("li");
                // add message to the elements text
                li.innerText = message;
                // add to list of messages
                messageList.append(li);
        }

        // watch for socket to emit a 'message'
        socket.on("message", addMessageToHTML);

        // display message when a user connects
        function alertUserConnected() {
        addMessageToHTML("User connected");
        }


And server code:

CODE

socket.on("message", function(msg) {
                socket.broadcast.emit("message", msg);
console.log("message = ", msg);

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 2 2020, 12:11 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



I'm not familiar with websockets, but I noticed this:

QUOTE(Rayj00 @ Jun 2 2020, 02:38 AM) *

const form = document.querySelector("form");

The above should match all forms on the page, which means all of them will indeed use the defined textfield's value. You may have to use separate scripts, that each only targets its own HTML form.
Correction: querySelector only returns the first match. So if the page contains more than one form, I think the script should submit only the first form.

Also the following part seems incorrect, so the none of the forms should work:

QUOTE
const input = document.querySelector("msg");

(the querySelector() method looks for CSS selectors, like HTML elements, CLASS or ID values; but "msg" is none of that).

This post has been edited by Christian J: Jun 2 2020, 02:03 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 27th April 2024 - 02:46 AM