Click here to Skip to main content
11,479,170 members (63,395 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
I have written following Javascript code.
<body>
    <script>
        function hi(){
           alert("Hello World !!");
        }
 
        var no1 = document.createElement('input');
        no1.setAttribute('type', 'button');
        no1.setAttribute('name', 'one');
        no1.setAttribute('value', '1');
        document.body.appendChild(no1);
    
        no1.onClick = hi;
    
    </script>
</body>

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.

Thanks!
Posted 24-Dec-12 8:05am
Edited 24-Dec-12 9:13am
v5
Rate this: bad
good
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: http://www.w3schools.com/jsref/event_onclick.asp[^].

But you don't have to add the attribute explicitly with setAttribute. It could be just
no1.onclick=function(){
   // some JavaScript code...
};

Good luck,
—SA
  Permalink  
v2
Rate this: bad
good
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 Smile | :)
You know why? Because of the capital letter: no1.onClick = hi;
Just make it lowercase and will work (no1.onclick = hi;).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can make a asp.net 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.

[EDIT]
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"

<head>
    <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);
        }
    </script>
</head>
<body onload="AddButton ()">
    <div id="buttonContainer"></div>
</body>

http://help.dottoro.com/ljxhibki.php[^]
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 205
1 F-ES Sitecore 195
2 OriginalGriff 130
3 Frankie-C 120
4 DamithSL 95
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,366
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2015
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