Adrian
Adrian

Reputation: 2291

Printing or Echoing message in html page from javascript

I have the following script

function validateEmailp() {
var messagemail = document.getElementById('emailerrorz').innerText;
var two = document.getElementById('email').value;
var first = two.split("@")[1];
var badEmails = ["gmail.com", "yahoo.com"]
if (badEmails.indexOf(first) != -1) {
        document.getElementById("email").value = ""; //this works
    messagemail = 'We do not accept free e-mails'; //this doesn't
    return false;
    }   
return true;
}

and HTML

<td>{EMAILFIELD}<span id="emailerrorz"></span></td>

and {EMAILFIELD} is in PHP

<input id="email" class="txt" type="text" name="email" size="25" value="" maxlength="255" onblur="validateEmailp()"></input>

But it doesn't work for me on printing the error in the span id. It only works on resetting the value from there.

Upvotes: 1

Views: 39

Answers (2)

georg
georg

Reputation: 214959

Properties don't work this way. You want:

 document.getElementById('emailerrorz').innerText = 'We do not accept free e-mails'

or

  var messagemail = document.getElementById('emailerrorz');
  ....
  messagemail.innerText = etc

http://jsfiddle.net/MJXEg/

Upvotes: 1

Sergio
Sergio

Reputation: 28837

When you do var messagemail = document.getElementById('emailerrorz').innerText; your variable stores a string with that content.

When you var messagemail = document.getElementById('emailerrorz'); your variable stores a object/element and then you can use the property .innerText

So use:

var messagemail = document.getElementById('emailerrorz');
// rest of code
messagemail.innerText = 'We do not accept free e-mails';

Upvotes: 1

Related Questions