1

I want to direct the button to another page

function showInput() {
    document.getElementById("demo").innerHTML = '<input type="submit" 
    onclick="location.href = "addnewcustomer.php";" id ="newcust" 
    class="btnRegister" value = "New Customer">';
}

I want when i click the New Customer button, it will redirect to addnewcustomer.php but when i run the code, nothing happened.

There are no errors. Just the button didn't function the way i want it to

6 Answers 6

1

You have to escape quotes in element. Try following code

function showInput() {
    document.getElementById("demo").innerHTML = '<input type="submit" onclick="location.href = \'addnewcustomer.php\';" id ="newcust" class="btnRegister" value = "New Customer">';
}
<a onclick="showInput()" >Add New Buton</a>
<div id="demo"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

You can use following code to redirect page.

function showInput() {
    document.getElementById("demo").innerHTML = '<input type="submit" id ="newcust" class="btnRegister" value = "New Customer" />';
}

document.getElementById("newcust").onclick = function () {
    location.href = "addnewcustomer.php";
}
<div id="demo"></div>
<a onclick="showInput()" >Click To Get Button</a>

Comments

0

Try this

function showInput() {
 document.getElementById("demo").innerHTML = '<input type="submit" onclick="location.href = \'https://www.youtube.com/\';" class="btnRegister" value = "New Customer">';
}
showInput()
<p id="demo">click</p>

Comments

0

It is because you didn’t close your input tag Try this...

function showInput() {
document.getElementById("demo").innerHTML = '<input type="submit" 
 onclick="location.href =     "addnewcustomer.php";" id ="newcust" 
 class="btnRegister" value = "New Customer"/>';
}

If you don’t close it the button will not work.

Comments

0

function showInput() {
    document.getElementById("demo").innerHTML = '<input type="submit" onclick="location.href = "yourpage.php";" id ="newcust" class="btnRegister" value = "New Customer">';
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a onclick="showInput()" >Click To Get Button</a>
<p id="demo"></p>
Right Click And inspect Button.

1 Comment

I didn’t understand the space doesn’t matter
0

Try the code below, the inner html text is missing with escape string. Call window.onload to trigger your method and display your button. HTH.

<head>
<script type="text/javascript">
    function showInput() {
        document.getElementById("demo").innerHTML = '<input type="submit" onclick="location.href = \'addnewcustomer.php\';" id ="newcust" class="btnRegister" value = "New Customer">';
    }
    window.onload = showInput;
    </script>
</head>
<body>
<p id="demo"></p>
</body>

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.