Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a java-script function which will generate buttons dynamically in my html page.
My java-script is:
JavaScript
<script type="java/script">
            
            function myFunc(target_div,num_buttons) {
                var buttons = "";
                for (var i = 0; i < num_buttons; i++) {
                    //                             added class ----------------v
                    buttons += '<input type="button" id="button_' + i + '" class="mybuttons" value="button_' + i + '"></input>';
                }
                target_div.html(buttons);
                var doButtonPress = function(i) {
                    alert(i); // I actually do something more complicated here, but it's not important now
                }

                //yes, mybuttons exist 
                $('.mybuttons').click(function() {
                    doButtonPress(this.id.replace('button_', ''));
                    //this.id.replace('button_', '') <- returns i value
                });
            }
        </script>

And in the HTML is:
HTML
<body  önload="myFunc(this,'10');">
        <form>
            <div id="mydiv" ></div>
        </form>
        
        <?php
        // put your code here
        ?>
    </body>

I don't know why this not calling the myFunc(this, '10'). Is the any problem to pass id and number by my code ?
Posted

First the body tag should be like this.


HTML
<body  önload="myFunc(this,'10');">

this should be like this

<body  onload="myFunc(this,'10');">

you write "önload" it should be "onload"
 
Share this answer
 
add this
JavaScript
$(function(){
     $('.mybuttons').click(function() {
          doButtonPress(this.id.replace('button_', ''));
    //this.id.replace('button_', '') <- returns i value
     });
     })
 
Share this answer
 
Try this
JavaScript
function myFunc(target_div,num_buttons) {
	function getBtn(i) {
		var btn = document.createElement("input");
		btn.setAttribute("type","button");
		btn.setAttribute("value","button_"+i);
		btn.className = "mybuttons";
		btn.setAttribute("id","button_"+i);
		return btn;
	}
	function doButtonPress(k){
		alert(k);
	}
	for(var i=0; i<num_buttons; i++) {
		var btn = getBtn(i);
		btn.onclick = function(){
			doButtonPress(this.id.replace('button_', ''));
		}
		target_div.appendChild(btn);
	}
}

If you are adding the id and class to the button for using it with doButtonPress() only then following might be an easier solution.
JavaScript
function myFunc(target_div,num_buttons) {
	function doButtonPress(k){
		alert(k);
	}
	function getBtn(i) {
		var btn = document.createElement("input");
		btn.setAttribute("type","button");
		btn.setAttribute("value","button_"+i);
		btn.specialProperty = i; //Just add a special property
		btn.onclick = function(){
			doButtonPress(this.specialProperty);
		}
		return btn;
	}
	for(var i=0; i<num_buttons; i++) {
		target_div.appendChild(getBtn(i));
	}
}


Accompanying HTML
HTML
<div id="mydiv" ></div>
<script type="text/javascript">
	myfunc(document.getElementById("mydiv"),10);
</script>
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900