1

I want to add a new text directly after the two html labels(Email: and Password:) with javascript. How do I go about it?

Just directly after Email: and Password: inside the two labels.

<div class="container">
        <div class="welcome-area">
            <div class="content">
                <!--<h1><strong>SOME TEXT GOES HERE</strong></h1>
                <div class="login-area">
                    <div class="app-intro">

                    </div>
                    <div class="login-div">
                        <form action="add-to-cart.html">
                            <h1>LOGIN</h1>
                            <label for="email">Email:</label>
                            <input id="email" type="email" placeholder="Email" autofocus autocomplete="">
                            <label for="password">Password:</label>
                            <input id="password" type="password" placeholder="Password">
                            <input type="submit" value="Submit">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
camelCase
  • 475
  • 5
  • 14
  • 1
    What Javascript have you tried? Please post the effort you have made so we can take a look and better help you. – Peter Feb 12 '19 at 14:02
  • You can use [appendChild](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild) – Oram Feb 12 '19 at 14:03
  • 2
    Possible duplicate of [How to insert an element after another element in JavaScript without using a library?](https://stackoverflow.com/questions/4793604/how-to-insert-an-element-after-another-element-in-javascript-without-using-a-lib) – Oram Feb 12 '19 at 14:06

3 Answers3

1

You can try with querySelector() with attribute selector to modify the textContent property:

document.querySelector('[for="email"]').textContent += ' email text'; // Email label
document.querySelector('[for="password"]').textContent += ' password text'; // Password label
<div class="container">
    <div class="welcome-area">
        <div class="content">
            <h1><strong>SOME TEXT GOES HERE</strong></h1>
            <div class="login-area">
                <div class="app-intro">

                </div>
                <div class="login-div">
                    <form action="add-to-cart.html">
                        <h1>LOGIN</h1>
                        <label for="email">Email:</label>
                        <input id="email" type="email" placeholder="Email" autofocus autocomplete="">
                        <label for="password">Password:</label>
                        <input id="password" type="password" placeholder="Password">
                        <input type="submit" value="Submit">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Try something like that :

var value_to_add = "my code here"; document.getElementById("password").after(value_to_add);

Ice Ax
  • 152
  • 1
  • 10
0

One way of doing it, probably not the best answer, I'm sure there are other more efficient methods.

Add ID's to the two labels

<div class="container">
    <div class="welcome-area">
        <div class="content">
           <h1><strong>SOME TEXT GOES HERE</strong></h1>
            <div class="login-area">
                <div class="app-intro">

                </div>
                <div class="login-div">
                    <form action="add-to-cart.html">
                        <h1>LOGIN</h1>
                        <label for="email" id="lblEmail">Email:</label>
                        <input id="email" type="email" placeholder="Email" autofocus autocomplete="">
                        <label for="password" id="lblPassword">Password:</label>
                        <input id="password" type="password" placeholder="Password">
                        <input type="submit" value="Submit">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var lblEmail = document.getElementById("lblEmail");
    var lblPass = document.getElementById("lblPassword");

    lblEmail.innerHTML += 'Some extra text here';
    lblPass.innerHTML += 'Some extra text here';
</script>

So for ex. validation you can do something like this

<script>
    var lblEmail = document.getElementById("lblEmail");
    var lblPass = document.getElementById("lblPassword");

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

    if (email == null){
        lblEmail.innerHTML += 'This field is required';
    }else if (pass == null){
        lblPass.innerHTML += 'This field is required';
    }else{
        ...do something here
    }       
</script>
Quintin
  • 103
  • 1
  • 9