Get the first word from sentence field

Hi ,

I am new in javascript. I need a code that get only the first word from the sentence field and make the word in lowercase onblur .
fayezsaadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sys_ProgCommented:
Hi fayezsaad,

Use indexOf() to search space and use substring() to get the first word
Use toLowerCase() to change to lowercase
append the remaining string to this changed value and assign it back to the control

http://www.w3schools.com/jsref/jsref_obj_string.asp

Cheers!
0
smaccariCommented:
Here is an example though:

<input type=text onblur="changeCase(this)"/>

<script>
function changeCase(obj)
{
   var str=obj.value;
   var firstWord=str;
   if (str.indexOf(" ")!=-1) firstWord=str.substring(0,str.indexOf(" "));
   firstWord=firstWord.toLowerCase();
   var newValue="";
   if (str.indexOf(" ")!=-1) newValue=firstWord+str.substr(str.indexOf(" "));
   else newValue=firstWord;
   obj.value=newValue;
}
</script>
0
TimYatesCommented:
Or you can do it with Regexp if the mood takes you ;-)

<html>
  <body>
    <script type="text/javascript">
  function doit( aTextField )
  {
    var aTxt = aTextField.value ;

    var words = aTxt.match( /(\S+).*/ ) ;
    if( words && words.length > 1 )
    {
      aTxt = aTxt.replace( /\S+/, words[ 1 ].toLowerCase() ) ;
      aTextField.value = aTxt ;
    }
  }
    </script>
    <textarea onblur="doit( this )"></textarea>
  </body>
</html>
0
REA_ANDREWCommented:
here is a function which you can specify which word you would like to get

<script language="javascript">
function getWord(str,pos)
{
      var SplitString = str.split(" ");
      alert(SplitString[parseInt(pos)-1]);
}
</script>

an example of this in action is here

<html>

<head>
<script language="javascript">
function getWord(str,pos)
{
      var SplitString = str.split(" ");
      alert(SplitString[parseInt(pos)-1]);
}
</script>

<title>New Page 1</title>
</head>

<body>

<script language="javascript">
      getWord("this is a test STring",3)
</script>

</body>

</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fayezsaadAuthor Commented:
Thanks All for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.