1

Page one has a "Request Feedback" button. Page two has a email form with different <.option> for feedback, comment, questions.

Is there a way to make it so that when you click the "Request Feedback" button, it redirects you to Page two and automatically sets the <.option> to feedback?

<select name="subject" id="subject">
  <option value="feedback">Feedback</option>
  <option value="comment">Comment</option>
  <option value="question">Questions</option>
</select>
Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
KeyKey
  • 11
  • 1

2 Answers2

0

Page 1 :

<a href="page2.html?q=feedback"> Request Feedback</a>

Page 2 :

const q = urlParams.get('q')
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

document.getElementById("subject").value = q;
<select name="subject" id="subject">
                                <option value="feedback">Feedback</option>
                                <option value="comment">Comment</option>
                                <option value="question">Questions</option>
</select>
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
0

The answer of Avinash was very helpfull. The only thing was missing was the urlParams. I have updated his code, hope this helps!

Page 1:

<a href="page2.html?q=feedback"> Request Feedback</a>

Page 2:

<select name="subject" id="subject">
   <option value="feedback">Feedback</option>
   <option value="comment">Comment</option>
   <option value="question">Questions</option>
</select>

<script>
      const queryString = window.location.search;
      const urlParams = new URLSearchParams(queryString);
      const q = urlParams.get('q')
      function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
      }

      //This is for default value of the select
      if (q) {
        document.getElementById("subject").value = q; 
      }
</script>
Luuk
  • 1
  • 1