1

I have login script where are inputs email and password. Email input value is set "email" but when It's not correct i added a script which change value to "email not correct" but i wanted it have color red. Code below do not work. How do i change it?

<input id="email" type="text" name="logemail" placeholder="Email"> echo '<span class=\'wrong\'> <script> document.getElementById("email").value = "incorrect email"; </script> </span>'; 

CSS:

.wrong{ color:red; } 
6
  • Which part fails? The CSS or the PHP? Commented Sep 20, 2014 at 12:29
  • Why is the script element inside that span? Where is the element with id="nick"? Commented Sep 20, 2014 at 12:30
  • Can you give the whole code including your markup? Commented Sep 20, 2014 at 12:30
  • I added piece of code from form input and edited this "nick" it was my mistake. Commented Sep 20, 2014 at 12:32
  • It works for me, what's the matter? Commented Sep 20, 2014 at 12:33

2 Answers 2

2

Your CSS changes the value of text in the span element not the input value of the text box. Technically speaking, your current code only changes the colour of the "innerHTML" of the span element not the "value" of the input element.

Change

.wrong{ color:red; } 

To

#email{ color:red; } 

To get the desired behaviour...

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help and patience :) But this code will change color of value forever. And im trying to do it red only when it is incorrect which means only when the echo occures
Then the easy solution would be to add the class "wrong" to the input element instead of the span!
1

Use separate javascript function to add class dynamically.

<script> function addClass(element, myClass) { element.className += ' ' + myClass; } </script> <span class='wrong1'> <script> document.getElementById("email").value = "incorrect email"; var foo = document.getElementById('email'); addClass(foo, 'wrong'); </script> </span> 

1 Comment

This sounds great, ill check that now. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.