0

I am using style="text-transform:capitalize" so the get the first letter of an word to uppercase. But now I am facing the problem that each 1st letter of each word is converted to uppercase. How to prevent this?

So helle new world becomes Hello new world

Original code:

<input type="text" class="form-control" id="bedrijfsnaam" name="bedrijfsnaam" value="" style="text-transform:capitalize">
Muiter
  • 1,470
  • 6
  • 26
  • 39
  • Possible duplicate of [Auto Capitalize ONLY the First Letter of Each Word in an Input Field](https://stackoverflow.com/questions/41048336/auto-capitalize-only-the-first-letter-of-each-word-in-an-input-field) – Raghu Dec 10 '17 at 13:42
  • Please refer https://stackoverflow.com/questions/41048336/auto-capitalize-only-the-first-letter-of-each-word-in-an-input-field which already has an excellent answer. – Raghu Dec 10 '17 at 13:43

6 Answers6

2

Not sure about pure CSS solution, but this simple js could help:

<input type="text" class="form-control" id="bedrijfsnaam" name="bedrijfsnaam" value="">

field=document.getElementById('bedrijfsnaam');

field.onkeyup = function(){
str=this.value;
this.value=str[0].toUpperCase()+str.slice(1,str.length);

};

Snippet:

field=document.getElementById('bedrijfsnaam');

field.onkeyup = function(){
str=this.value;
this.value=str[0].toUpperCase()+str.slice(1,str.length);

};
<input type="text" class="form-control" id="bedrijfsnaam" name="bedrijfsnaam" value="">
sinisake
  • 11,240
  • 2
  • 19
  • 27
0

You can capitalize the first letter of the #bedrijfsnaam element by using the pseudo-element :first-letter

 #bedrijfsnaam:first-letter{
      text-transform: capitalize; 
      }
Rahele Dadmand
  • 573
  • 1
  • 4
  • 17
0

You can try this

#bedrijfsnaam {
  text-transform: lowercase;
}

#bedrijfsnaam:first-letter {
    text-transform: uppercase;
}
Kingsley Solomon
  • 481
  • 1
  • 4
  • 9
0
<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

        <script>
        $(document).ready(function(){
            $('input').keypress(function(){
                var $this = $(this),
                val = $this.val();
                val = val.substr(0, 1).toUpperCase() + val.substr(1);
                $this.val(val);
            });
        });

        </script>

    </head>

    <body>

        <input type='text' name='name' class='name' placeholder='Enter your name here'/>

    </body>
</html>
0
#bedrijfsnaam::first-letter,
#bedrijfsnaam:first-letter {
    text-transform: uppercase;
}

Explanation:

  • Old browsers cannot understand ::first-letter (with 2 double colon), but can understand :first-letter (with 1 double colon). So the above code should work with most browsers.
  • We can't apply ::first-letter directly to the HTML element's style because it requires state (which is ::first-letter)
evilReiko
  • 19,501
  • 24
  • 86
  • 102
0
selector::first-letter:{
text-transform: uppercase
}

The ::first-letter CSS pseudo-element applies styles to the first letter of the first line of a block-level element, but only when not preceded by other content (such as images or inline tables). Style First Letter

Ram
  • 1