1

I am having trouble in solving my previous post. I only got a solution which requires jQuery, but I want to use JavaScript instead.

Really need help on this.

Previous Post (For Reference) : HTML - How To Display & Direct Selected Values From Drop Down Menu To A New HTML Page

This is the first jquery codes.

<input type="submit" id="btngo" value="Go" />
<script type="text/javascript">
  $(function() {
    $("#btngo").bind("click", function() {
      var url = "Page2.htm?foodbeverage=" + encodeURIComponent($("#foodbeverage").val()) + "&status=" + encodeURIComponent($("#status").val());
      window.location.href = url;
    });
  });
</script>

This is the second jquery codes.

< script type = "text/javascript" >
  var queryString = new Array();
$(function() {
  if (queryString["foodbeverage"] != null && queryString["status"] != null) {
    var data = "<b>Food Beverages:</b> " + queryString["foodbeverage"] + " <b>Dine Takeaway:</b> " + queryString["status"];
    $("#showdata").html(data);
  }
}); <
/script>

4

2 Answers 2

1

I've converted the first snippet to a native JS equivalent. I didn't add the status to the URL, it is just more of the same as getting the food beverage.

(function() {
  /**
   * Handles the click of the submit button.
   */
  function onSubmitClicked(event) {
        // Get the input element from the DOM.
    var beverage = document.getElementById('foodbeverage'),
        // Get the value from the element.
        beverageValue = beverage.value,
        // Construct the URL.
        url = 'Page2.html?foodbeverage=' + encodeURIComponent(beverageValue) + '&status=test';

    // Instead of going to the URL, log it to the console.
    console.log('goto to URL: ', url);
  }
  
  // Get the button from the DOM.
  var submitButton = document.getElementById('btngo');
  // Add an event listener for the click event.
  submitButton.addEventListener('click', onSubmitClicked);
})();
<label>
Beverage:
<input type="text" id="foodbeverage"/>
</label>
<input type="submit" id ="btngo" value="Go" />

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a bunch, man. I just realized that I had to change the the 'console.log'. Extremely sorry.
I've outlined some steps to take in your newer question, I hope it will help you to figure out what to do next.
0

For the second code snippet, add a div to the form/page. The JS function below will update the div value on window onload with parameters passed. Note that div is only updated if both the querystring paramaters are present.

         <div id="showdata"></div>

         <script type="text/javascript">
            window.onload = passParameters;

            //Function to update "showdata" div with URL Querystring parameter values
            function passParameters() {
                var foodbeverage = getParameterByName("foodbeverage");
                var status = getParameterByName("status");
                if (foodbeverage != null && status != null) {                
                    var data = "<b>Food Beverages:</b> " + foodbeverage + " <b>Dine Takeaway:</b> " + status;
                    document.getElementById("showdata").innerHTML = data;
                }
            }
            //Get URL parameter value
            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, " "));
            }
        </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.