Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to convert lowercase to upper case

What is wrong with the following htmla and javascript code

formToConvert.html

<html>
    <head>
        <title>ExampleToConvert</title>
        <script type = "text/javascript" src = "con.js"></script>
    </head>
    <body> 
        <form id ="myform">
            <input type = "text" id = "field1" value = "Enter text Here"/><br/>
            <input type ="submit" value = "submit" onclick = "convert()"/>
        </form>
    </body> 
</html>

con.js

function convert() 
{
    var str ;
    str = document.getElementById("field1");
    document.writeln(str.toUpperCase());
}

Why is the above code not giving me the desired result?

like image 591
station Avatar asked Aug 10 '11 14:08

station


People also ask

How do I convert a lowercase string to uppercase?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.

How do you change capital letters to lowercase in JavaScript?

JavaScript String toLowerCase() The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.

Is there a capitalize method in JavaScript?

The toUpperCase() method converts the string to uppercase.

What is toUpperCase () an example of in JavaScript?

JavaScript's toUpperCase() method converts a string object into a new string consisting of the contents of the first string, with all alphabetic characters converted to be upper-case (i.e. capitalized). Note: JavaScript strings are immutable.


3 Answers

Try:

str = document.getElementById("field1").value;

This is because getElementById returns a reference to your HTML Element, not the "text"-value that is contained.

like image 99
GNi33 Avatar answered Oct 14 '22 02:10

GNi33


You need to change it to this:

var str = document.getElementById("field1").value;
document.writeIn(str.toUpperCase());
like image 43
James Johnson Avatar answered Oct 14 '22 02:10

James Johnson


The following change should fix your issue:

str = document.getElementById("field1");

should be

str = document.getElementById("field1").value;
like image 21
Rion Williams Avatar answered Oct 14 '22 00:10

Rion Williams