Amal.A
Amal.A

Reputation: 39

How to change textContent via javascript?

I have a registration form page, and there is empty div i want to use to display errors. To check forms before triggering php script i use javascript:

function errorHandler(){
    var loginIn;
    var passIn;


    loginIn = document.forms["regForm"]["login"].value;
    passIn = document.forms["regForm"]["password"].value;

    if (loginIn == "" || loginIn == null) {
        alert("LOGIN CANNOT BE EMPTY");
        return false;
    } 
}


It works fine, and alert message do appear when i call them like this:

<form name="regForm" action= "save_user.php" onsubmit="return errorHandler()" method="post">.

But there is as i mentioned before a div inside of the form:

div id ="errorArea"></div> and when i try to put a text inside of this div like this:

function errorHandler(){

    var loginIn;
    var passIn;
    var erorAreaMessage;

    loginIn = document.forms["regForm"]["login"].value;
    passIn = document.forms["regForm"]["password"].value;
    erorAreaMessage = document.getElementById('errorArea').textContent;

    if (loginIn == "" || loginIn == null) {
        erorAreaMessage = "LOGIN CANNOT BE EMPTY";
        return false;
    }
}

Nothing happens, can someone explain me why?

Upvotes: 1

Views: 18879

Answers (1)

Kamehameha
Kamehameha

Reputation: 5473

You need to put the value inside the <div>. That can done by setting the innerHTML or textContent property to your error-message. Try this -

...
if (loginIn == "" || loginIn == null) {
    document.getElementById('errorArea').textContent = "LOGIN CANNOT BE EMPTY";
    return false;
}}

Upvotes: 4

Related Questions