How to insert a line break with javascript?
Asked Answered
F

5

30

The Submit Form I have has the name, email, and phone number fields. Of course, I want to insert line break between the three. I tried to insert a line break between all three, but only generated one at the end of the form, when my code should appended 3 line breaks, not one.

The generated view source shows this:

<label>Name: </label><input required="" name="fullName" type="text"><label>Email: </label>   <input required="" name="email" type="text"><label>Phone Number: </label><input required="" name="phoneNumber" type="text"><br><input value="Submit Query" type="submit"></form>

My Javascript Code to produce this form:

function queryForm()
{
    var queryBox = document.getElementById("queryBox").style.display = "block";
    var queryForm = document.getElementById("queryForm");
    var linebreak = document.createElement("br");

    var lblName = document.createElement("label");
    lblName.textContent = "Name: ";
    queryForm.appendChild(lblName);

    var fullName = document.createElement("input");
    fullName.name = "fullName";
    fullName.type = "text";
    fullName.required = "required";
    queryForm.appendChild(fullName);
    queryForm.appendChild(linebreak);


    var lblEmail = document.createElement("label");
    lblEmail.textContent = "Email: ";
    queryForm.appendChild(linebreak);
    queryForm.appendChild(lblEmail);

    var email = document.createElement("input");
    email.name = "email";
    email.type = "text";
    email.required = "required";
    queryForm.appendChild(email);


    var lblPhoneNumber = document.createElement("label");
    lblPhoneNumber.textContent = "Phone Number: ";
    queryForm.appendChild(linebreak);
    queryForm.appendChild(lblPhoneNumber);

    var phoneNumber = document.createElement("input");
    phoneNumber.name = "phoneNumber";
    phoneNumber.type = "text";
    phoneNumber.required = "required";
    queryForm.appendChild(phoneNumber);


    var submitQuery = document.createElement("input");
    submitQuery.type = "submit";
    submitQuery.value = "Submit Query";
    queryForm.appendChild(linebreak);
    queryForm.appendChild(submitQuery);
}
Flirtatious answered 8/3, 2014 at 17:31 Comment(0)
E
55

You should create new <br> tag each time when you will append it, something like

linebreak = document.createElement("br");
queryForm.appendChild(linebreak);

DEMO

Exegesis answered 8/3, 2014 at 17:40 Comment(0)
T
9

You should try to append each time a new node, and not the same one that was previously created, i.e:

queryForm.appendChild(document.createElement("br"));

EDIT: the explanation is here on the documentation

Node.appendChild Adds a node to the end of the list of children of a specified parent node. If the node already exists it is removed from current parent node, then added to new parent node.

https://developer.mozilla.org/en-US/docs/Web/API/Node.appendChild

Taxation answered 8/3, 2014 at 17:34 Comment(0)
C
9

The problem with your code is you're inserting the same element over and over again.

Here's an analogy: Imagine you have several children lined up in a row. Yes you can attempt to move one of them after each of the other children, however, at the end of the day you still only have one of that child. He's going to end up at the end of the row.

Solution: Create the line break element each time you need to insert it.

Also, here's an obligatory plug for jQuery: http://www.jquery.com

Carcass answered 8/3, 2014 at 17:39 Comment(0)
B
3

Protip: Append your inputs to your labels. That way a user can click the label to get focus on the input.

This has the additional perk that you can simply apply the CSS display:block to your labels and have them on separate lines!

Blackmail answered 8/3, 2014 at 17:33 Comment(0)
I
1

You can use the classic \n to get this to work. I used this solution for a multi-line textarea in my project, but I cut out some code to simplify the example:

explanation.value = 'Equation: ' + equation;

explanation.value += '\nAnswer: ' + answer;

enter image description here

Isosceles answered 27/10, 2020 at 18:10 Comment(1)
That didn't work for me. This failed: $('.weather').replaceWith(weather + '\n');Incognito

© 2022 - 2024 — McMap. All rights reserved.