javascript - How to style placeholder text color for individual text field? - Stack Overflow

I would like to style placeholder text in different color for each text field.The code from mozilla doc

I would like to style placeholder text in different color for each text field.

The code from mozilla doc below will affect entire input tag.

/%3a-moz-placeholder

<style type="text/css">  
    input:-moz-placeholder {  
      color: green;  
    }  
</style> 

I want to have different colors for each text field.

HTML

<input id="foo" type="text" placeholder="im green" />
<input id="foo2" type="text" placeholder="im red />

jQuery

I tried below but it will just set the color to the input text field itself not to the placeholder.

var elementId = "#foo2";
$(elementId + ":-moz-placeholder").css("color", "red");

I think my selector is not specified correctly though not sure how to set properly.

How do I style individual placeholder by element id using jQuery?

I would like to style placeholder text in different color for each text field.

The code from mozilla doc below will affect entire input tag.

https://developer.mozilla/en/CSS/%3a-moz-placeholder

<style type="text/css">  
    input:-moz-placeholder {  
      color: green;  
    }  
</style> 

I want to have different colors for each text field.

HTML

<input id="foo" type="text" placeholder="im green" />
<input id="foo2" type="text" placeholder="im red />

jQuery

I tried below but it will just set the color to the input text field itself not to the placeholder.

var elementId = "#foo2";
$(elementId + ":-moz-placeholder").css("color", "red");

I think my selector is not specified correctly though not sure how to set properly.

How do I style individual placeholder by element id using jQuery?

Share Improve this question edited Feb 17, 2012 at 4:07 Meow asked Feb 17, 2012 at 3:19 MeowMeow 19.2k52 gold badges143 silver badges183 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Have you tried... CSS classes by any chance?

<input class="green" id="foo" type="text" placeholder="im green" />
<input class="red" id="foo2" type="text" placeholder="im red />

<style type="text/css">  
    input.green:-moz-placeholder {  
      color: green;  
    }  
</style> 

:-moz-placeholder is a pseudo class.

Pseudo classes are not part of the DOM, so jQuery can not directly edit anything about them.

An alternate approach would be to use classes to change colors, which is what Walkerneo suggested.

Take his code, and then use this to switch the class from green to red:

    $("input#foo").toggleClass('red').toggleClass('green');

It's possible to prepend a block with jQuery:

    var $inlineStyleTemplate = $("" +
            "<style id='custom-menu-color'> " +
                ".navigation-md-lg > li > a, .header-right > ul > li > a,.header-right .fa-search {color: " + menuColor + "}" +
                "svg {fill: " + menuColor + "}" +
                "#search-search {border-color: " + menuColor + "}" +
                "#search-search::-webkit-input-placeholder { color: " + menuColor + "}" +
                "#search-search::-moz-placeholder { color: " + menuColor + "}" +
                "#search-search:-ms-input-placeholder { color: " + menuColor + "}" +
                "#search-search:-moz-placeholder { color: " + menuColor + "}" +
            "</style>"
        );

    $('head').prepend($inlineStyleTemplate);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742323543a4422296.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信