0

According to this: Change an HTML5 input's placeholder color with CSS The code to the change the placeholder color is:

::-webkit-input-placeholder { /* WebKit, Blink, Edge */
    color:    #909;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
   color:    #909;
   opacity:  1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
   color:    #909;
   opacity:  1;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
   color:    #909;
}

This code is really good and works fine. However, I have a small problem. I wants to change the color of the placeholder when someone clicking "submit" (The only problem is the function that change it on js... Everything else working good). This is what I tried to do with js:

$("#email").css('::-webkit-input-placeholder', 'red');
$("#email").css(':-moz-placeholder', 'red');
$("#email").css('::-moz-placeholder', 'red');
$("#email").css(':-ms-input-placeholder', 'red');

It doesn't works to me... What should I change to fix it? I guess it's something small, but I can't fix it... Any suggestions?

Community
  • 1
  • 1
Web O
  • 103
  • 2
  • 9
  • better to make a class with all above css and add that class in js by using addClass(myclass) – Vicky Kumar Dec 26 '16 at 16:19
  • When Submit is clicked **the page is submitted** – Jonas Wilms Dec 26 '16 at 16:20
  • @Jonasw My code just reperesents a message of successful or not in the same page. The changing color is if something wrong – Web O Dec 26 '16 at 16:22
  • @WebO `::-webkit-input-placeholder` is not a CSS property. It is part of the `selector`. `color` and `opacity` are the properties here. And again, if you click submit, the form will be submitted unless it is an AJAX request. – Arun Kumar Mohan Dec 26 '16 at 16:24

2 Answers2

1

Instead of changing the CSS using JS, you can add a class on the form & inherit your properties into it. Something like addClass or toggleClass on your form tag when someone submits the form.

JS:

$('.form-btn').on('click', function(e) {
  e.preventDefault();
  $(this).parent().closest('form').toggleClass('submit-form');
});

CSS (inherited with .submit-form class):

form.submit-form ::-webkit-input-placeholder {
  /* WebKit, Blink, Edge */
  color: red;
}

form.submit-form :-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: red;
  opacity: 1;
}

form.submit-form ::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: red;
  opacity: 1;
}

form.submit-form :-ms-input-placeholder {
  /* Internet Explorer 10-11 */
  color: red;
}

Have a look at the snippet below:

$('.form-btn').on('click', function(e) {
  e.preventDefault();
  $(this).parent().closest('form').toggleClass('submit-form');
});
input[type="email"] {
  display: block;
  font-size: 20px;
  margin-bottom: 10px;
}

::-webkit-input-placeholder {
  /* WebKit, Blink, Edge */
  color: #909;
}

:-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: #909;
  opacity: 1;
}

::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: #909;
  opacity: 1;
}

:-ms-input-placeholder {
  /* Internet Explorer 10-11 */
  color: #909;
}

form.submit-form ::-webkit-input-placeholder {
  /* WebKit, Blink, Edge */
  color: red;
}
form.submit-form :-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: red;
  opacity: 1;
}
form.submit-form ::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: red;
  opacity: 1;
}
form.submit-form :-ms-input-placeholder {
  /* Internet Explorer 10-11 */
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="" action="">
  <input type="email" placeholder="Enter Email" />
  <button class="form-btn">Submit (change placeholder color)</button>
</form>

Hope this helps!

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41
0

Hope this works !!

$("#email").addClass('success');
.success::-webkit-input-placeholder {
  color: orange;
}
.success::-moz-placeholder{
  color: orange;
}
.success:-ms-input-placeholder{
  color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="email" id="email" placeholder="email" />
xxCodexx
  • 438
  • 1
  • 3
  • 15