-1

I do not know much javascript and I am trying to pull in a form's input values, construct the link to pass the values to and send the user to that link on button click.

I am not sure how to make both of those actions happen onClick.

Here is what I currently have:

<script>
function myFunction() {
  var firstname = document.getElementById("firstname").value;
  var lastname = document.getElementById("lastname").value;
  var email = document.getElementById("email").value;
  var phone = document.getElementById("phone").value;
  var link = 'https://app.squarespacescheduling.com/schedule.php\?owner=21917237&appointmentType=20129186&firstName=' + firstname + '&lastName=' + lastname + '&email=' + email + '&phone=' + phone;
}
</script>

And this is what I am trying inline with my button element:

<div class="de elBTN elAlign_center elMargin0 ui-droppable de-editable" id="tmp_button-86409" data-de-type="button" data-de-editing="false" data-title="button" data-ce="false" data-trigger="none" data-animate="fade" data-delay="500" style="margin-top: 30px; outline: none; cursor: pointer; display: block;" aria-disabled="false" data-elbuttontype="1">
<a onclick={myFunction(), onclick="location.href=link;"} class="elButton elButtonSize1 elButtonColor1 elButtonRounded elButtonPadding2 elBtnVP_10 elButtonCorner3 elBtnHP_25 elBTN_b_1 elButtonShadowN1 elButtonTxtColor1 elButtonFull" style="color: rgb(255, 255, 255); font-weight: 600; background-color: rgb(1, 116, 199); font-size: 26px;">
<span class="elButtonMain"><i class="fa_prepended fa fa-angle-double-right"></i>Let's Get Started<i class="fa_appended fa fa-angle-double-left"></i></span>
<span class="elButtonSub"></span>
</a>
</div>

I need to send the user to the link created in var link. What's the best way to have this happen?

Thanks!

Preston G
  • 69
  • 6
  • 1
    Does [this](https://stackoverflow.com/questions/3910736/how-to-call-multiple-javascript-functions-in-onclick-event) help? – Phoenix Feb 11 '21 at 14:22
  • 1
    You can do all you need inside first function, just add redirect at end of it. – ikiK Feb 11 '21 at 14:24

2 Answers2

1

Group the actions in one function can satisfy your need.

//function declaration
<script>
function myFunction() {
  var baseUrl = 'https://app.squarespacescheduling.com/schedule.php\owner=21917237&appointmentType=20129186'
  
  var data = {
    firstname: document.getElementById("firstname").value,   //if using ts, can add ? for checking the existence of ```document.getElementById('firstname')?.value
    lastname: document.getElementById("lastname").value,
    email: document.getElementById("email").value,
    phone: document.getElementById("phone").value,
  }
  
  //do some checking
  var unvalidElem = Object.values(data).filter(eachValue => eachValue !== undefined || eachvalue !== null)

  //if all elements valid, then go to the link
  if(unvalidElem.length === 0){
      location.href = `${baseUrl}&firstName=${firstname}&lastName=${lastname}&email=${email}&phone=${phone}`
  }
</script>


//actually invoke
<a onclick={myFunction();"}
tcf01
  • 1,699
  • 1
  • 9
  • 24
1

Add this at the bottom of your function, once you've constructed your url:

   location.href = "yourUrl";
mrcrag
  • 310
  • 3
  • 16