0

I have a form with several radio buttons that have 3 associated values:

  • Red (Highest Priority): link.com/3
  • Orange (Higher Priority): link.com/2
  • Green (Working): link.com/1

I need to trigger redirects to different links based on the users selections, if the user selects answers which are all associated to the ID #sCgreen then the redirect works. What I need to do is override this:

  • If the user selects any orange answers (#sCorange) this should override the green redirect;
  • If the user selects any red answers (#sCred) this should override both the green and the orange redirect;

Is there a way to prevent each event from firing if the higher priority event is triggered, or to prioritise the events accordingly?

  $('#continue').click(function(event) {

    event.preventDefault()
    
    // Red triggered.
    if(document.getElementById('sCred').checked) {
      window.open("https://link.com/3","_self")
      return;
    }
    // End red triggered.
    
    // Orange triggered.
    if(document.getElementById('sCorange').checked) {
      window.open("https://link.com/2","_self")
      return;
    }
    // End orange triggered.
    
    // Green triggered.
    if(document.getElementById('sCgreen').checked) {
      window.open("https://link.com/1","_self")
    }
    // End green triggered.

  });

Any help would be greatly appreciated!

EDIT: added return;, there are multiple instances of the input ID's so this only works on the first 3 instances of the questions.

van
  • 158
  • 1
  • 2
  • 12
  • Put a `return;` after the first two `window.open` commands to return out of the function. You don't have to put one after the last one as there is no further logic you would want to prevent. – Taplar Jul 31 '20 at 23:10
  • @Taplar thank you! - is there a way to do this when there are multiple uses of the #sCgreen, #sCorange and #sCred in the same form? – van Jul 31 '20 at 23:23
  • You can't have multiple `#sCgreen`. Ids are expected to be unique. If you are repeating ids, you need to change them to be classes instead, and convert the logic to work with classes. – Taplar Jul 31 '20 at 23:25
  • Do you know how would I override even with multiple ID's/classes? The problem is that the user can select green answers, but if they select just one orange or red answer, these should supercede the green answers – van Jul 31 '20 at 23:27
  • https://stackoverflow.com/questions/59887834/getting-the-value-of-the-input-field-using-jquery/59888053#59888053 <= related answer where I have information about converting repeating id logic to using classes instead – Taplar Jul 31 '20 at 23:28

0 Answers0