0

I'm doing fine with this code but would like to have a submit button that will help me submit a form in addition to it. From this, I also want to get the value of input text through request.vars in controller.

this.typeInput = function(callbackFn) {
    var inputWrap = document.createElement("div")
    inputWrap.className = "input-wrap"
    var inputText = document.createElement("textarea")
    inputText.setAttribute("placeholder", "Ask me anything...")
    inputWrap.appendChild(inputText)
    inputText.addEventListener("keypress", function(e) {
          // register user input
          if (e.keyCode == 13) {
            e.preventDefault()
            typeof bubbleQueue !== false ? clearTimeout(bubbleQueue) : false // allow user to interrupt the bot

When I try to add a function as inputText.setAttribute('type','submit') i get a blank screen. Am I missing something

2
  • 1
    You got an error as you tried to set type attribute on textarea, not button. Create a button element and then set an 'submit' attribute on it. Commented Aug 4, 2018 at 9:28
  • 1
    It is good practice to single javascript line of code with a semi-colon ; Commented Aug 4, 2018 at 9:41

3 Answers 3

3

Your code is creating textarea not button so Replace,

var inputText = document.createElement("textarea");

with

var button= document.createElement("button");
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the solution to your problem:

this.typeInput = function (callbackFn) {
    var inputWrap = document.createElement("div");
    inputWrap.className = "input-wrap";

    var inputText = document.createElement("textarea");
    inputText.setAttribute("placeholder", "Ask me anything...");
    inputWrap.appendChild(inputText);
    inputText.addEventListener("keypress", function(e) {
        // register user input
        if (e.keyCode == 13) {
            e.preventDefault();
            typeof bubbleQueue !== false ? clearTimeout(bubbleQueue) : false;
        }
    }
    var btn = document.createElement("button");
    btn.setAttribute("type", "submit");
    inputWrap.appendChild(btn);
    btn.addEventListener("click", function(e) {
        e.preventDefault();
        typeof bubbleQueue !== false ? clearTimeout(bubbleQueue) : false;
    }
}

7 Comments

still getting a blank page
@loved_by_Jesus If you could provide more code(HTML, JavaScript etc.) then it would help clarify your problem
Sorry I had a typo somewhere
The button is working well. How ever i have just noted that when I refresh the page, next comes a blank screen with the input area and button still present. I'll post the whole code shortly
cant post the whole code but you can find it here :github.com/dmitrizzle/chat-bubble/blob/master/examples/…
|
0

var inputText = document.createElement("textarea") is creating a textarea, you need to create a button t have the type='submit"

var btn = document.createElement("button") btn.setAttribute("type", "button")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.