0

I tried to follow this example;

Set to true state a checkbox in a different page(javascript)

but can't get it to work, not sure how to fix it?

Getting error message;

Request Method: GET Request URL:
http://localhost:8000/profileusers/register.html?chk=1"

First Page - terms.html


<div class="row">
         <a href="{% url 'register' %}" target="_blank">
            <input type="button" name="acceptButton" class="btn accept-button" value="Accept" id="acceptButton" onclick="registerCheck(this.id)">
        </a>
        <a href="{% url 'register' %}">
            <input type="button" name="declineButton" id="declineButton" class="btn decline-button" value="Decline">
        </a>
    </div>

    <script>
        function registerCheck(clicked_id) {
            if (clicked_id === 'acceptButton') {
                window.location.href = "register.html?chk=1";
            }
        }
    </script>
    

Second Page - register.html

 
<div class="form-check">
         <input class="form-check-input" type="checkbox" id="terms-box" name="terms-box">
         <label class="form-check-label" for="flexCheckDefault"> &nbsp;
             <a href="{% url 'terms' %}" class="link-terms">Terms & Conditions</a>
         </label>
     </div>


     <script>
     var url = window.location.href.split("?");
     if (url[1].toLowerCase().includes("chk=1")) {
       $('#terms-box').attr('checked', true);
     }
     </script>

Liah Cheston
  • 33
  • 1
  • 4
  • use **console.log()** will help you figure it out why is not working. Like **console.log(url)** after `var url window.location.href.split("?"); ` – Patfreeze Aug 27 '21 at 17:59
  • try using [URLSearchParams](https://developer.mozilla.org/ru/docs/Web/API/URLSearchParams) for extracting query params. And maybe pass script instructions to DOMContentLoaded event callback. – capchuck Aug 27 '21 at 18:42

0 Answers0