How to show the result of a conditional Java Script structure in a text box in html using jquery?

Asked

Viewed 95 times

0

  var textoUser = $("#txtFieldUser");
  var textoGanhador = $("txtFieldGanhador");


  var computerChoice = Math.random();

  if (computerChoice < 0.34) {
        computerChoice = "Pedra";

  } else if(computerChoice <= 0.67) {
        computerChoice = "Papel";
  } else {
        computerChoice = "Tesoura";
  } 

  console.log("Computer: " + computerChoice);

  var compare = function (choice1, choice2) {
      if (choice1 === choice2)
        return ("O resultado é um empate!");
      else if (choice1 === "pedra") {
      if (choice2 === "tesoura")
            return ("pedra vence");
        else {
            return ("papel vence");
        }
    }
    else if (choice1 === "papel") {
        if (choice2 === "pedra")
            return "papel vence";
        else {
            return "tesoura vence";
        }
    }
    else if (choice1 === "tesoura") {
        if (choice2 === "pedra")
            return "pedra vence";
        else {
            return "tesoura vence";
        }
    }
};


    compare(textoUser, computerChoice);
   $('#txtFieldComputer').ready(function() {
       $('#txtFieldComputer').val(computerChoice);
   });
  • You just create the input and place the attribute id with the value txtFieldComputer being like this: <input type="text" id="txtFieldComputer" />

  • 1

    The problem is in ready that is too much. the $('#txtFieldComputer').val(computerChoice); alone does what you want. The ready is for document, when ready, to be used like this $( document ).ready(function() {

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.