syera rasyad
syera rasyad

Reputation: 25

Onclick function button inside a javascript

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

Upvotes: 1

Views: 318

Answers (6)

Aiyaz Khorajia
Aiyaz Khorajia

Reputation: 598

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>

Upvotes: 1

Akjun
Akjun

Reputation: 370

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>

Upvotes: 0

TarangP
TarangP

Reputation: 2738

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.

Upvotes: 0

omkaartg
omkaartg

Reputation: 2787

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.

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

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>

Upvotes: 1

Paritosh Mahale
Paritosh Mahale

Reputation: 1316

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>

Upvotes: 0

Related Questions