Submitting a form after alert box message

513 views
Skip to first unread message

Melissa Brooks

unread,
Mar 25, 2021, 2:48:58 AM3/25/21
to oTree help & discussion
Hi Chris and others,

I have a JavaScript event that occurs on button click and the button cannot be clicked unless a choice is made (via radio buttons). I have a form with two radio buttons:
<form id="thisForm" align="center" float="right">
<input type="radio" id="lottery1" name="choice0" value="1" align="center" >A
<input type="radio" id="lottery2" name="choice0" value="2" align="center" >B
<input type="hidden" id="payoffr1" name="payoffr1" value=" " />
 and this is the code for the button:<input id="start_button" type="button" value="Start" onClick="startEvent();"> 

In models.py I have: 
choice0 = models.IntegerField()

After the function startEvent, there is another function that follows and displays a javaScript generated variable in the alert box:
function Prize(indicatedSlice)
        {
            payoffr1 = indicatedSlice.text.slice(-3);
            document.getElementById("payoffr1").value = payoffr1;
            alert("You have won " + payoffr1);
        }
        document.getElementById('thisForm').submit();
        } 

I tried to submit the form after clicking 'OK' on the alert box message by adding the last line in the function Prize, but it keeps me in the same page. How can I submit the form after a user clicks 'OK'? Any advice would be extremely helpful! 

Chris @ oTree

unread,
Mar 25, 2021, 3:15:31 AM3/25/21
to Melissa Brooks, oTree help & discussion
I don’t recommend creating your own <form> element. oTree already adds one automatically. 

Sent from my phone

On Mar 25, 2021, at 2:49 PM, Melissa Brooks <melibr...@gmail.com> wrote:

Hi Chris and others,
--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/7bff3029-bf03-4c9e-a761-a8c76e0be09dn%40googlegroups.com.

Melissa Brooks

unread,
Mar 25, 2021, 12:11:29 PM3/25/21
to oTree help & discussion
Chris, thank you so much! You and your team have been extremely helpful. I am very grateful this group exists. 

I didn't know this. I inspected the page and I was able to see the id was simply "form" so I replaced it with "thisForm" in my function and it submitted. Thanks again!

Reply all
Reply to author
Forward
0 new messages