0
<input type="text" name="" placeholder="nd"><span hidden="hidden" id = "hi" style="color: red">*</span>


<script>
    

    jQuery(document).ready(function(){
        
        var c = $("#hi").text();
         $(c).css('color','red');
        
  var content = jQuery('input:text').attr('placeholder');

  jQuery('input:text').attr('placeholder',content+c);
});
</script>

I have placed input text with a placeholder and using jquery script I am trying to add * immediately after the placeholder's text which should have red color but my code is not working. The asterisk mark is showing in the placeholder but it is of color black not of red.

  • `c` is text, so `$(c)` probably not doing what you want – Jaromanda X Oct 13 '20 at 22:36
  • I tried with c.css('color', 'red') but still, it is not working – Divya Borse Oct 13 '20 at 22:42
  • Does this answer your question? [2 colors in one placeholder of input field](https://stackoverflow.com/questions/24069830/2-colors-in-one-placeholder-of-input-field) – showdev Oct 13 '20 at 23:03
  • Also see [change only the asterisk in placeholder to red](https://stackoverflow.com/questions/50429681/change-only-the-asterisk-in-placeholder-to-red) and [Red Asterisk directly beside placeholder in input box](https://stackoverflow.com/questions/29292520/red-asterisk-directly-beside-placeholder-in-input-box) and [How to get asterisk in placeholder with css](https://stackoverflow.com/questions/37537205/how-to-get-asterisk-in-placeholder-with-css). – showdev Oct 13 '20 at 23:05
  • I saw the above codes but they have created placeholder using CSS by adding padding(left) etc. I want to add text with red * using jquery in the placeholder – Divya Borse Oct 13 '20 at 23:14
  • You can't include a red (styled) asterisk as part of the placeholder because the [placeholder attribute is plain text and does not accept HTML markup](https://stackoverflow.com/a/27134234/924299). I recommend using a [method like this](https://stackoverflow.com/a/24076889/924299) to overlay labels on inputs and style them to look like placeholders. In your context, maybe [labels are more appropriate than placeholders](https://www.smashingmagazine.com/2018/06/placeholder-attribute/) anyway. – showdev Oct 17 '20 at 22:48

0 Answers0