<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        function doSomething() {
            alert("works");
        }
            
        window.onload = function() {
        
            var btn = document.createElement("input");
            btn.type = "button";
            btn.value = "click";
            btn.onclick = "doSomething();";
            document.body.appendChild(btn);
            
        }
</script>
</head>
<body>

</body>
</html>

What is the problem?

Recommended Answers

All 9 Replies

should be:

btn.onclick = doSomething;

should be:

btn.onclick = doSomething;

please try before posting a solution.
The solution to the problem is to create html string and set it to container tag's innerHTML. If you try appendChild method, it does not see the functions.

>>please try before posting a solution.
I've done that time and time again. What I suggested works. But just to humor you, I tested it and it still works. So YOU must be doing something wrong on your end. You need to assign a function reference, NOT a string. Whatever work around you came up with, it's not the ideal solution.

you gotta be kidding me!
try this and see it doesnt work :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        function doSomething() {
            alert("works");
        }
            
        window.onload = function() {
        
            var btn = document.createElement("input");
            btn.type = "button";
            btn.value = "click";
            btn.onclick = "doSomething";
            document.body.appendChild(btn);
            
        }
</script>
</head>
<body>
</body>
</html>

i am sorry i had to remove double quotes too :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        function doSomething() {
            alert("works");
        }
            
        window.onload = function() {
        
            var btn = document.createElement("input");
            btn.type = "button";
            btn.value = "click";
            btn.onclick = doSomething;
            document.body.appendChild(btn);
            
        }
</script>
</head>
<body>
</body>
</html>

now it works, thanks :)

But, how will you pass a parameter to that function if it requires any?

i am sorry i had to remove double quotes too :

Yes you do. If you leave the double quotes, you are NOT assigning a function reference. You are assigning a string. In the future, instead of replying with a condescending response ("please try before posting a solution") do make an effort to read what what ACTUALLY suggested. Clearly I did NOT suggest those quotes.

But, how will you pass a parameter to that function if it requires any?

In that case you use an anonymous function and call your function from within the anonymous function: btn.onclick = function(){doSomething("This is the param you are passing");};

commented: this guy is smart +3

wow great information from a junior poster :)
Thank you!!

you are welcome. Take care.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.