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?
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?
Share Improve this question edited Oct 27, 2017 at 19:35 Luke T Brooks 5571 gold badge8 silver badges27 bronze badges asked Aug 10, 2011 at 14:05 stationstation 7,14516 gold badges59 silver badges93 bronze badges 3- 1 What result does it give you? – rlemon Commented Aug 10, 2011 at 14:07
- People gets reputation to downvote innocent question, why buddies? – SMI Commented Feb 4, 2014 at 12:29
- @SMI: I didn't down vote this question, but the one person who did probably did so because the OP didn't specify the difference between the expected result and the actual result. In other words, they didn't identify the unwanted symptom(s). – Jeromy French Commented Apr 21, 2015 at 16:46
3 Answers
Reset to default 7Try:
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;