how can i onClick again ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mostashaar
    New Member
    • Apr 2010
    • 1

    how can i onClick again ?

    Hello,
    I have this code how can i make it onClick each time it visits the function:
    Code:
    <html>
        <head>
        <script>
    	C();
    	function C()
    	{	
    		document.write("<input type='button' value='click me!' onclick='check()'/>");
    	}
    	function check()
    	{
    		alert("YO");
    		C();
    	}
    	</script>
    	</head>
    	<body></body>
    </html>
    Last edited by Dormilich; Apr 28 '10, 07:52 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    not at all.

    that is, not with onclick. because document.write( ) will wipe out your page if called after the page finished loading.

    you would have to use DOM or .innerHTML to make that happen.

    Code:
    <html>
        <head>
        <title>don't forget me, I'm required</title>
        <script type="text/javascript">
    var MyCode = {
    	author : "Bertold von Dormilich",
    	created : "2010-04-28",
    	coverage : "http://bytes.com/"
    };
    // create button through DOM/JS
    // note, we need only 1 instance
    MyCode.button = (function () {
    	var inp = document.createElement("input");
    	inp.type = "button";
    	inp.value = "click me!";
    	return inp;
    }());
    // create a function to be called onclick
    MyCode.createButton = function () {
    	// duplicate button
    	var button = MyCode.button.cloneNode(true);
    	// add DOMEvent
    	button.addEventListener("click", MyCode.createButton, false);
    	// append to <body>
    	document.body.appendChild(button);
    };
        </script>
        </head>
        <body>
        <script type="text/javascript">
    MyCode.createButton();
        </script>
        </body>
    </html>
    note: for the sake of simplicity, this won’t work in IE.

    Comment

    Working...