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?

amarinediary
  • 4,930
  • 4
  • 27
  • 45
novika
  • 3
  • 1
  • 3

3 Answers3

0

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

  • Im new to javascript, Can you please write the exact code? the sumit button its type, not a class. – novika Aug 24 '21 at 08:29
  • Try this: https://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 –  Aug 24 '21 at 09:41
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>
Ski3r3n
  • 19
  • 7
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>
Ski3r3n
  • 19
  • 7