0

I'm trying to set up an html web page that depends on the user entering a certain word in one text box to make another text box display a different word. Ultimately the use of this feature will become more complex but for now I just want to make that simple routine work. I've written code but it doesn't seem to follow the logic. When I click on the button no matter what is in text1 both text1 and text2 show "Dog" and "Cat" respectively. Here's my code that isn't working:

enter code here
<head>
<title></title>
</head>
<body> 
<script type="text/javascript">
  function myFunction() {
      if (Text1.value = "Dog") 
      {
          Text2.value = "Cat";
      }
      else
      {
          Text2.value = "Mouse";
      }
  }
</script>
<br><br><br><br>
<font size=6 face=arial color=#336699>
<b>TestRoutine</b>
<br><br>
</font>
<input id="Button1" type="button" value="button" onclick="myFunction()"> 
<input id="Text1" type="text" />
<input id="Text2" type="text" />
</body>
</html>

3 Answers 3

2
  1. You never define Text1. You want var Text1 = document.getElementById('Text1');
  2. = is an assignment. You need to use === for a comparison (or == for a comparison with type coercion)
Sign up to request clarification or add additional context in comments.

Comments

2

You need to replace Text1.value and Text2.value with:

document.getElementById("Text1").value

and

document.getElementById("Text2").value

respectively.

Also, when making comparisons in your if statement, you should you === instead of = to actually compare the values.

Edit: You should actually use === instead of ==.

2 Comments

DOMNode.value is always a string so you don't need the type coercion from ==. Use ===.
Thanks, I still don't have a full understanding of when to use === vs ==. The latter == would still work in this scenario, but I guess the former is better.
0

I fixed it thanks to the suggestions and tried not to fix the parts that i do not like.

<html>
<head>
<title></title>
</head>
<body> 
<script type="text/javascript">
  function myFunction() {
      if (document.getElementById("Text1").value === "Dog") 
      {
          document.getElementById("Text2").value = "Cat";
      }
      else
      {
          document.getElementById("Text2").value = "Mouse";;
      }
  }
</script>
<br><br><br><br>
<font size=6 face=arial color=#336699>
<b>TestRoutine</b>
<br><br>
</font>
<input id="Button1" type="button" value="button" onclick="myFunction()"> 
<input id="Text1" type="text" />
<input id="Text2" type="text" />
</body>
</html>

3 Comments

Great. Now I want to make the if statement link to another page instead of filling Text2. The other page would be something like FileDownload.htm.
I found it!parent.document.location.href = "Docx.htm";
What I meant to say is that I found it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.