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>
<开发者_JAVA技巧/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?
Try:
str = document.getElementById("field1").value;
This is because getElementById returns a reference to your HTML Element, not the "text"-value that is contained.
You need to change it to this:
var str = document.getElementById("field1").value;
document.writeIn(str.toUpperCase());
The following change should fix your issue:
str = document.getElementById("field1");
should be
str = document.getElementById("field1").value;
精彩评论