-1

Firstly, this is my first time working with JavaScript so it is probably just me missing something very simple.

All I want to do is get the value of a text box and make an alert with the first letter capitalised. I have everything working, the first letter isn't actually being capitalised.

This is how I am calling the function:

else
{
    input = document.getElementById("search").value;
    'input'.search();
    alert(input);
}

This is the function it self:

function search(string)
{
    return string.charAt(0).toUpperCase();
}
UsainBloot
  • 816
  • 6
  • 20
  • You are calling your function on a text literal, and you are not doing anything with its return value. – CBroe May 20 '15 at 11:59
  • 3
    possible duplicate of [Capitalize the first letter of string in JavaScript](http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript) – Anonymous May 20 '15 at 11:59

7 Answers7

2

this is the correct code

else
{
    input = document.getElementById("search").value;
    input =search(input);
    alert(input);
}
user3227295
  • 2,168
  • 1
  • 11
  • 17
0

Try this:

input = search(input);

Instead of:

'input'.search();
StudioTime
  • 22,603
  • 38
  • 120
  • 207
0

You aren't changing input at all. Try:

else
{
    input = document.getElementById("search").value;
    alert(search(input));
}

If you want to permanently change input Try:

else
{
    input = document.getElementById("search").value;
    input = search(input);
    alert(input);
}
hounvs
  • 9
  • 3
0

A couple of things wrong here,

firstly,

search(input)

instead of

'input'.search()

secondly,

string is immutable so you'll have to do,

input = search(input);
alert(input);
.
.
.
function search(str) {
    return (str.replace(str.charAt(0), str.charAt(0).toUpperCase()));
}
painotpi
  • 6,894
  • 1
  • 37
  • 70
0

Your question is not pretty clear to me, but I assume that you want, if your input string is "hello" , you want the output as "Hello".

Try this

call the method and alert the message:-

alert(search(input));

in the method:-

return Character.toUpperCase(input.charAt(0)) + input.substring(1);
Newinjava
  • 972
  • 1
  • 12
  • 19
0
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase()+this.slice(1);
}

var searchField = document.getElementById("search");
var query = searchField.value;
searchField.value = query.capitalize();

function search(query) {
   // query api or something...
}
num8er
  • 18,604
  • 3
  • 43
  • 57
-1

The answers are correct, but you will have only the first letter in the alert box. If you don't want only the first letter, here is the solution:

function search(string)
{
    return string.substring(0,1).toLocaleUpperCase() + string.substring(1);
}

From https://stackoverflow.com/a/30123193/2379208

Community
  • 1
  • 1
Kaymaz
  • 454
  • 8
  • 23
  • It would be better to flag it as a duplicate than to take an answer from the question I linked and post it. – Anonymous May 20 '15 at 12:04