Click here to Skip to main content
12,892,320 members (48,531 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
I have written following Javascript code.
        function hi(){
           alert("Hello World !!");
        var no1 = document.createElement('input');
        no1.setAttribute('type', 'button');
        no1.setAttribute('name', 'one');
        no1.setAttribute('value', '1');
        no1.onClick = hi;

I have registered my event with the function hi(), but it is not working.
Is there any error in my code?

I would like that whenever I click on my button, it show me the alert box.

Posted 24-Dec-12 7:05am
Updated 24-Dec-12 8:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You never tried to create a single attribute responsible for events, which in this case should be onclick attribute:[^].

But you don't have to add the attribute explicitly with setAttribute. It could be just
   // some JavaScript code...

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The problem is, tat this way the generated code looks like this:
<input submitname="one" type="button" onclick="function hi(){ alert(" hello="" world="" value="1" />

I suppose, this is not what you want :)
You know why? Because of the capital letter: no1.onClick = hi;
Just make it lowercase and will work (no1.onclick = hi;).
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can make a object button or image button, and add the attribute onClientClick

So you make the button, press F4, in the property onClientClick enter "hi(); return false;" without the double quotes.

return false cancels the button postback event. You can do return hi();

and if hi returns true, the postback event fires, and if hi() returns false, the postback is canceled.

Just remember to register your java script file first.

To fix your code above, you need to create the element correctly first for it to work. Don't use setAttribute, JavaScript has everything you need to build the element.

var inputObj = document.createElement ("input");
inputObj.type = "button"

    <script type="text/javascript">
        function AddButton () {
            var button = document.createElement ("input");
            button.type = "button";
            button.value = "Create a new button";
            button.onclick = AddButton;
            var container = document.getElementById ("buttonContainer");
            container.appendChild (button);
<body onload="AddButton ()">
    <div id="buttonContainer"></div>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100