How do I set the form dropdown value through javascript?

Respondidas


Publicado 16 ene 2024

Purpose

  • Show an alert telling the user they need to be logged in when selecting certain form types in the request type drop-down
  • Redirect them to the login page if they click accept in the alert dialog
  • Stay on the same page and change nothing if they decline

Progress

  • Show alert to the user when they select one of the specific form types while being anonymous/not logged in
  • Don't redirect to the request form url when the alert shows
  • Redirect to the login page when they accept the alert

Problems

  • When the cancel the alert, they are still on the same page, but the drop-down value is showing the wrong value
    • If they had no form selected, the drop-down looks wrong and behaves weirdly
    • If they had another form selected, the drop-down looks wrong, and while everything else looks the same, if the user tries to send in the form, it fails. It fails, because the form thinks it's the new form but the user only filled in the fields shown on the screen, which is of the original form.
  • Can't figure out how to change the drop-down value back to the original value through code. None of the standard way of doing this through JavaScript is working.

Example Code

This being a stripped down version of my code, most of this works fine, everything except the line 31: "dropdown.value = previousForm;"
Which I also have issues with just doing in the console; any tries to modify the selected drop-down value through code, will not do anything. Trying to see the value of the select element in the console correctly shows what I'm setting through code, but what the user sees is completely different/wrong.

Example Images

Image 1

After selecting "I want to report a bug or crash" while not having any form selected, and then declining the alert dialog that shows up, I end up in this state

See console that everything looks as it should, but the drop-down is behaving weirdly

Image 2

Moreover, if I then open the drop-down, the "I want to report a bug or crash" is highlighted in bold, and selecting it will not do anything. However, selecting "-" which is what I was on, and should currently be on, refreshes the page, most likely meaning the drop-down back-end believes I am on the "I want to report a bug or crash" form. (Which clearly I am not as no fields are showing, and I haven't been redirected)

Conclusion

I'm having an awful time trying to do any kind of manipulation of drop-downs in themes, and I'm at my wits end.
This is on the latest version of the official Copenhagen theme version 3.1.6, with very minimal modifications, only adding custom JavaScript alongside the existing JavaScript.

Any and all help is greatly appreciated!


0

3

0 comentarios

Iniciar sesión para dejar un comentario.

¿No encontró lo que buscaba?

Nueva publicación