0

I have the following newsletter Mailchimp form:

<div class="mc4wp-form-fields"><p>
    <label>Email: 
        <input type="email" name="EMAIL" placeholder="" required="">
</label>
</p>

<p>
    <input type="submit" value="Subscribe">
</p></div>

I need when someone clicks on the subscribe button, after 2 seconds to redirect in a new URL in a new tab. For example www.youtube.com

How is this possible to happen using javascript?

1
  • window.location = "www.youtube.com" will work perfectly Commented Aug 24, 2021 at 8:23

3 Answers 3

0

Change your button with following code, then put myFunction() function code in your file.

<input id="button" type="submit" name="button" onclick="myFunction();" value="Subscribe"/>


<script>
function myFunction(){
    setTimeout(function () { window.location = "www.youtube.com" }, 2000);
};   
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

To detect the click of a button, use this method. Then, to redirect the user after X seconds, look here.

2 Comments

Im new to javascript, Can you please write the exact code? the sumit button its <input> type, not a class.
Try this: codepen.io/Modz_/pen/KKqKxVX. It's supposed to work, if it doesn't report it to me, I'll try directly with a HTML page
0

For your subscribe button, you can do this:

<input type="submit" value="Subscribe" onclick="subscribe()"/*executes a command on click*/>

When you do this, you get the button to reference a javascript function.

Now, you can include the javascript function.

function subscribe(){
    setTimeout(function () {
          location.replace("https://youtube.com")/*replaces the website with youtube.com*/
    }
}, 2000 /*waits for 2000 miliseconds(2 seconds) before executing code in setTimeout*/);

Here it is in live code.

function subscribe() {
  setTimeout(function() {
    location.replace("https://youtube.com")/*replaces the website with youtube.com*/
  }, 2000 /*waits for 2000 miliseconds(2 seconds) before executing code in setTimeout*/ );
}
<div class="mc4wp-form-fields"><p>
    <label>Email: 
        <input type="email" name="EMAIL" placeholder="" required="">
</label>
</p>

<p>
    <input type="submit" value="Subscribe" onclick="subscribe()" /*executes a 
command on click*/>
</p></div>

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.