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?
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.
JavaScript String toLowerCase() The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.
The toUpperCase() method converts the string to uppercase.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With