Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Javascript button click listereners didn't redirect me to another page

i'm creating a shop cart and i started with a home page with 3 buttons I had a little problem with redirecting those buttons to another pages here's my html and javascript codes

I tried every possibility of redirection on javascript :(

function inscription(){
var myForm = "inscription-form.html";
document.getElementById("btn1f").addEventListener('click' , function () {
    window.location.replace(myForm);
});
}
// redirect my second button
var myShop = "http://www.google.com";
document.getElementById("btn2s").addEventListener('click' , function () {
    window.location.replace(myShop);
});
// redirect my third button
function redirect(){
var myGoogle = "http://www.google.com";
document.getElementById("btn3g").addEventListener('click' , function () {
    window.location.replace(myGoogle);
});
}
<section class="body-container">
        <div class="container-item title">
            <h1>Who said women need therapy &#128530;,
                <br> <br> We need shopping &#129297;
            </h1>
        </div>
        <div class="container-item buttons">
            <div class="button-1">
                <span>Create an account?</span>
                <button type="button" class="btn" id="btn1f">Sign In</button>
            </div>
            
            <div class="button-2">
                <span>You're a customer &#128526;</span>
                <button type="button" class="btn" id="btn2s">Go Shopping</button>
            </div>
            
            <div class="button-3">
                <span>Not Interested</span>
                <button onclick="redirect()" type="button" class="btn" id="btn3g">Go Back</button>
            </div>
            
        </div>
    </section>  

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

<!-- html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>title</title>
     <script src="js.js"></script> 
  </head>
  <body>
<section class="body-container">
        <div class="container-item title">
            <h1>Who said women need therapy &#128530;,
                <br> <br> We need shopping &#129297;
            </h1>
        </div>
        <div class="container-item buttons">
            <div class="button-1">
                <span>Create an account?</span>
                <button type="button" class="btn" id="btn1f">Sign In</button>
            </div>
            
            <div class="button-2">
                <span>You're a customer &#128526;</span>
                <button type="button" class="btn" id="btn2s">Go Shopping</button>
            </div>
            
            <div class="button-3">
                <span>Not Interested</span>
                <button type="button" class="btn" id="btn3g">Go Back</button>
            </div>
            
        </div>
    </section>  
  </body>
</html>

//below into js.js file

window.onload = function() {
    var myShop = "http://www.google.com";
    document.getElementById("btn2s").addEventListener('click' , function () {
        window.location.href = myShop;
    });   
    
    var myGoogle = "http://www.google.com";
    document.getElementById("btn3g").addEventListener('click' , function () {
        window.location.href = myGoogle;

    });    
    var myForm = "inscription-form.html";
    document.getElementById("btn1f").addEventListener('click' , function () {
        window.location.href = myForm;
    });    
}
about 4 years ago · Juan Pablo Isaza Report

0

You have warpped the click listeners inside named functions and call those function onclick. So First time you click on a button the listeners is being attached and the second time, the listener will work (however second click also attachs another listener). So omit one of onclick or addEventListener one of them is enough. I prefer deleting named functions and onclick and just attach listeners (They should be attached at the end of document or after window load)

window.onload=function(){

var myForm = "inscription-form.html";
document.getElementById("btn1f").addEventListener('click' , function () {
    window.location.replace(myForm);
});

var myShop = "http://www.google.com";
document.getElementById("btn2s").addEventListener('click' , function () {
    window.location.replace(myShop);
});

// redirect my third button
var myGoogle = "http://www.google.com";
document.getElementById("btn3g").addEventListener('click' , function () {
    window.location.replace(myGoogle);
});
}
<section class="body-container">
        <div class="container-item title">
            <h1>Who said women need therapy &#128530;,
                <br> <br> We need shopping &#129297;
            </h1>
        </div>
        <div class="container-item buttons">
            <div class="button-1">
                <span>Create an account?</span>
                <button type="button" class="btn" id="btn1f">Sign In</button>
            </div>
            
            <div class="button-2">
                <span>You're a customer &#128526;</span>
                <button type="button" class="btn" id="btn2s">Go Shopping</button>
            </div>
            
            <div class="button-3">
                <span>Not Interested</span>
                <button type="button" class="btn" id="btn3g">Go Back</button>
            </div>
            
        </div>
</section>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!