简体   繁体   中英

Change the placeholder color after submit the form

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?

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!

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" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM