0
I create two global variables and I would like that when sending the given form value of an input attribute to one of the variables and the other as set to false, receive true.
var escolhaPlayer1 = "";
var player1pronto = false;
$(document).ready( () =>{
$('#form1').on('submit', (event) => {
event.preventDefault();
escolhaPlayer1 = $("#form1 input[type=radio]:checked").val();
player1pronto = true;
});
console.log(escolhaPlayer1);
console.log(player1pronto);
})
But what happens is this, when I call these variables later, the values that were assigned within the form are not updated, and it shows the values that were set in their creation.
If I put the console.log()
inside the form to show the value, it picks, now when I put out does not come the value assigned.
I would like to know how to receive these form values in global variables.
OBS: I’ve tried to pass with data()
and localStorage()
, came to nothing. The only difference was that with the localStorage()
value shows, but only after refreshing the page.
but you are not submerged the form? this reloads the page and consequently reboots the variables
– Ricardo Pontual
I think I get it, but I still need the form to pass these values without updating, it’s like ?
– ijrdev
With the
event.preventDefault();
the page still restarts?– Pedro Roweder
It does not restart, because it prevents it. Then it may not pass the value to the variable. That’s where the question comes in that if this is the case, it would be possible to do it in some other way?
– ijrdev
I don’t think that’s the problem, even with Event.preventDefault(); values are assigned to variables.
– Pedro Roweder
I get it, so I have to work on the code so I can do it another way.
– ijrdev
It’s not a good thing to use global variables, try declaring as
let escolhaPlayer1
within the(document).ready
and assign values usingthis.escolhaPlayer1
. Let me know if it works.– Pedro Roweder
Nothing... What I’m trying to do is the following: It’s a simple game of jokenpô to practice jQuery. Player 1 and 2 have their forms that send their choices between stone, paper and scissors, this is where when they choose I pick the choosePlayer1 = (that comes the chosen option) and player1pronto = (see if he clicked the button to choose). And then I work with the data...
– ijrdev