www.webdeveloper.com
Results 1 to 4 of 4

Thread: Titlecase

  1. #1
    Join Date
    Mar 2012
    Posts
    1

    Titlecase

    Hello all. I am new to JavaScript and I am working on some code for an assignment. I am missing something but I am not sure what it is. Can anyone give a suggestion. I am not looking for the anwser handed to me but a clue to where I went wrong. Any help would be grateful.
    Thank you


    <script type="text/javascript">
    /* <![CDATA[ */
    function toTitleCase() {
    if (document.forms[0].sentenceField.value != "") {
    var sentence = document.forms[0].sentenceField.value;
    sentence = sentence.toLowerCase();
    var sentenceArray = sentence.split(" ");
    for (var i=0; i<sentenceArray.length; ++i) {
    var wordArray = sentenceArray.split("");
    wordArray[0] = wordArray[0].toUpperCase();
    sentenceArray = wordArray.join("");
    }
    document.forms[0].sentenceField.value = sentenceArray.join(" ");
    }
    }
    /* ]]> */
    </script>
    </head>


    <body>
    <form action="sentenceField" method="get" enctype="application/x-www-form-urlencoded">
    <p><input type="text" name="sentenceField" />
    <input type="button" value="Convert to Title Case" onclick="toTitleCase()" /></p>
    </form>

    </body>

  2. #2
    Join Date
    Nov 2010
    Posts
    1,031
    I would suggest having a look at your code using a debugger like firefox or Chrome's javascript console.

    This is something that can be better handled with a regex, but I guess your assignment isn't about that.

    Something worth noting is that once you have your separate words in an array, individual letters can be accessed by the charAt() method of the string object:
    http://www.w3schools.com/jsref/jsref_obj_string.asp

    meaning you don't have to split them again.

    you should also have a look at the replace() method of the string object.

  3. #3
    Join Date
    Jan 2011
    Posts
    117
    Code:
    !!"".toTitleCase?0:
    String.prototype.toTitleCase=
    /*b.b.Troy III p.a.e.*/
    function(x,i,a){ a=(x||this).match(/\S+/gi);i=a.length; 
    while(i--)a[i]=a[i].replace(a[i][0],a[i][0].toUpperCase());return a.join(" ")}
    Code:
    function toTitleCase(s,f) {
    f = document.forms[0].sentenceField; s = f.value;
    if (s != "") {
    s = s.toLowerCase();
    f.value = s.toTitleCase();
    }
    Last edited by Troy III; 03-26-2012 at 11:03 AM.

  4. #4
    Join Date
    Feb 2006
    Posts
    2,927
    You can use a simple replace to capitalize a sentence or each word in
    a heading. (pass a true argument to capitalize each word)


    Code:
    String.prototype.capit= function(deep){
        var str= this.toLowerCase(), rx= deep? /((^|\s+)[a-z])/g: /([a-z])/;
        return str.replace(rx, function(a, b){
            return b.toUpperCase();
        });
    }
    var s='the quick brown fox';
    s.capit()>> The quick brown fox
    s.capit(true)>>The Quick Brown Fox


    You can be a little more elaborate-

    Code:
    String.prototype.titleCap= function(){
    	var str= this.capit(true),
    	rx=/ +(a|an|and|at|by|for|from|if|in(to)?|is|it(\'?s)?|of|on|the|to|with(in|out)?) +/ig;
    	return str.replace(rx, function(w){
    		return ' '+w.toLowerCase()+' ';
    	});
    }
    var s='THE SEVEN VOYAGES OF SINBAD THE SAILOR';
    s.titleCap()>>The Seven Voyages of Sinbad the Sailor
    s.capit(true)>>The Seven Voyages Of Sinbad The Sailor
    Last edited by mrhoo; 03-26-2012 at 02:44 PM. Reason: simpler

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles