Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:31:47+00:00 2026-06-18T14:31:47+00:00

I am working with a dynamical form. When i enter my name in the

  • 0

I am working with a dynamical form. When i enter my name in the first textfield, and press enter, the next textfield pops up, requesting my email. however, i can click enter many times and the next textfield keeps popping up, which i only want to pop up once. The other problem, is that my next textfield, which requests my email, submits the form if i press enter twice, which i also want to prevent. Any solution?

Javascript

function addEvent(element, eventType, theFunction, capture) {
if (element.addEventListener) {
    element.addEventListener(eventType, theFunction, capture);
}
else if (element.attachEvent) {
    element.attachEvent("on" + eventType, theFunction);
}
}


function createEmailField() {
if ((window.event && window.event.keyCode == 13) || event.keyCode == 13) {

    var name = document.getElementById("name").value;

    if (name == "") {
        window.alert("Mata in ditt namn");
    }
    else if (name.search(/^[A-Za-z]+$/) == -1) {
        window.alert("Mata in ett namn med bokstäver");
    }
    else {

        var newDiv = document.createElement("div");

        var br = document.createElement("br");
        newDiv.appendChild(br);

        newDiv.appendChild(document.createTextNode("Hej " + name + "!"));

        var br = document.createElement("br");
        newDiv.appendChild(br);

        newDiv.appendChild(document.createTextNode("Hur når vi dig?"));

        var br = document.createElement("br");
        newDiv.appendChild(br);

        newDiv.appendChild(document.createTextNode("Epost:"));

        var br = document.createElement("br");
        newDiv.appendChild(br);

        var input = document.createElement("input");
        input.setAttribute("type", "text");
        input.setAttribute("id", "email");
        input.setAttribute("name", "email");
        newDiv.appendChild(input);

        var form = document.getElementById("form");
        form.appendChild(newDiv);

        var email = document.getElementById("email");
        addEvent(email, "change", createTextArea, false);

    }
}
}

function createTextArea() {

var email = document.getElementById("email").value;

if (email == "") {
    alert("Mata in ditt email");
}
else if (email.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) == -1) {
    window.alert("Mata in ett korrekt email");
}
else {
    var newDiv = document.createElement("div");

    var br = document.createElement("br");
    newDiv.appendChild(br);

    newDiv.appendChild(document.createTextNode("Vad har du för fråga om vår verksamhet?"));

    var br = document.createElement("br");
    newDiv.appendChild(br);

    var textArea = document.createElement("textArea");
    textArea.setAttribute("id", "question");
    textArea.setAttribute("name", "question");
    textArea.cols = "30";
    textArea.rows = "5";
    newDiv.appendChild(textArea);

    var br = document.createElement("br");
    newDiv.appendChild(br);

    newDiv.appendChild(document.createTextNode("Vi kommer att svara dig via: " +   email));

    var br = document.createElement("br");
    newDiv.appendChild(br);

    var button = document.createElement("input");
    button.setAttribute("type", "submit");
    button.setAttribute("value", "Skicka");
    button.setAttribute("name", "Skicka");
    button.setAttribute("id", "submit");
    newDiv.appendChild(button);

    var form = document.getElementById("form");
    form.appendChild(newDiv);

}
}

function prevent(event) {
if ((window.event && window.event.keyCode == 13) || event.keyCode == 13) {
    if (window.event) {
        window.event.returnValue = false;
    } else if (event.preventDefault) {
        event.preventDefault();
    }
}
}


function init() {
var name = document.getElementById("name");
addEvent(name, "keyup", createEmailField, false);
name.onkeypress = prevent;
}

window.onload = init;

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
<title>Inl1-3</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="Uppg3.js" >
</script>

</head>

<body>

<h2> Frågeformulär </h2>
<form id="form" method="post"     action="http://student.ts.mah.se/da123aht11/echoscript.php">
<div>
    Vad heter du?... 
    <br /><input type="text" name="name" id="name" /> 
</div>
</form>


</body>

</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:31:48+00:00Added an answer on June 18, 2026 at 2:31 pm

    A solution is to keep a status variable to check whether the email field/text area is created like var emailCreated = false, textareaCreated = false; and check the status and update them once the action is done.

    function addEvent(element, eventType, theFunction, capture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, theFunction, capture);
    }
    else if (element.attachEvent) {
        element.attachEvent("on" + eventType, theFunction);
    }
    }
    
    
    function createEmailField() {
    if ((window.event && window.event.keyCode == 13) || event.keyCode == 13) {
        if(emailCreated){
            return;
        }
    
        var name = document.getElementById("name").value;
    
        if (name == "") {
            window.alert("Mata in ditt namn");
        }
        else if (name.search(/^[A-Za-z]+$/) == -1) {
            window.alert("Mata in ett namn med bokstäver");
        }
        else {
    
            var newDiv = document.createElement("div");
    
            var br = document.createElement("br");
            newDiv.appendChild(br);
    
            newDiv.appendChild(document.createTextNode("Hej " + name + "!"));
    
            var br = document.createElement("br");
            newDiv.appendChild(br);
    
            newDiv.appendChild(document.createTextNode("Hur når vi dig?"));
    
            var br = document.createElement("br");
            newDiv.appendChild(br);
    
            newDiv.appendChild(document.createTextNode("Epost:"));
    
            var br = document.createElement("br");
            newDiv.appendChild(br);
    
            var input = document.createElement("input");
            input.setAttribute("type", "text");
            input.setAttribute("id", "email");
            input.setAttribute("name", "email");
            newDiv.appendChild(input);
    
            var form = document.getElementById("form");
            form.appendChild(newDiv);
    
            var email = document.getElementById("email");
            addEvent(email, "change", createTextArea, false);
    
            emailCreated = true;
    
        }
    }
    }
    
    function createTextArea() {
        if(textareaCreated){
            return;
        }
    
    var email = document.getElementById("email").value;
    
    if (email == "") {
        alert("Mata in ditt email");
    }
    else if (email.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) == -1) {
        window.alert("Mata in ett korrekt email");
    }
    else {
        var newDiv = document.createElement("div");
    
        var br = document.createElement("br");
        newDiv.appendChild(br);
    
        newDiv.appendChild(document.createTextNode("Vad har du för fråga om vår verksamhet?"));
    
        var br = document.createElement("br");
        newDiv.appendChild(br);
    
        var textArea = document.createElement("textArea");
        textArea.setAttribute("id", "question");
        textArea.setAttribute("name", "question");
        textArea.cols = "30";
        textArea.rows = "5";
        newDiv.appendChild(textArea);
    
        var br = document.createElement("br");
        newDiv.appendChild(br);
    
        newDiv.appendChild(document.createTextNode("Vi kommer att svara dig via: " +   email));
    
        var br = document.createElement("br");
        newDiv.appendChild(br);
    
        var button = document.createElement("input");
        button.setAttribute("type", "submit");
        button.setAttribute("value", "Skicka");
        button.setAttribute("name", "Skicka");
        button.setAttribute("id", "submit");
        newDiv.appendChild(button);
    
        var form = document.getElementById("form");
        form.appendChild(newDiv);
    
        textareaCreated = true;
    
    }
    }
    
    function prevent(event) {
    if ((window.event && window.event.keyCode == 13) || event.keyCode == 13) {
        if (window.event) {
            window.event.returnValue = false;
        } else if (event.preventDefault) {
            event.preventDefault();
        }
    }
    }
    
    
    function init() {
    var name = document.getElementById("name");
    addEvent(name, "keyup", createEmailField, false);
    name.onkeypress = prevent;
    }
    
    
    var emailCreated = false, textareaCreated = false;
    window.onload = init;
    

    Demo: Fiddle

    Another way is to use a removeEvent() function

    function removeEvent(element, eventType, theFunction, capture) {
    if (element.removeEventListener) {
        element.removeEventListener(eventType, theFunction, capture);
    }
    else if (element.detachEvent) {
        element.detachEvent("on" + eventType, theFunction);
    }
    }
    

    Then call removeEvent(document.getElementById("name"), "keyup", createEmailField, false); and removeEvent(document.getElementById("email"), "change", createTextArea, false); after adding the email field and textarea respectively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Each user can dynamically create a table from a form. I am trying to
I am working on a project which needs to extract text form a predefined
I hope someone can help me..... i am trying to build a dynamic form
I have a complex form that has a static section and one that can
I have a form which the user can dynamically add and remove a set
I am working on a dynamically nested form using the cocoon gem. I have
I am working on rendering a dynamic form in an ASP.NET MVC view that
I working in Java Swing and I am generating a dynamic form with control
I am working on a dynamic form in which I need to create a
I am working on very complex and dynamic form where I do lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.