| | |
adding button click event in javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 144
Reputation:
Solved Threads: 0
hi
my code is using javascript to dynamically build an html table and fill the table with html buttons.
my code:
my question is how can i add an eventhandler for a button click for every button.
i want to add an onClick javascript function to be performed on every button click and send the buttons id as parameter.
i tried to add this line inside the loop:
newcol.children[0].onclick = onClick(newcol.children[0].id);
but it doesn't seems to work.
my code is using javascript to dynamically build an html table and fill the table with html buttons.
my code:
javascript Syntax (Toggle Plain Text)
<html> <head> <title>javascript1</title> </head> <script lnaguage="javascript"> function Init() { var table = document.createElement("tbody"); var newrow,newcol,tmp; var array = new Array(16); for (var i=0 ; i<16 ; i++) array[i] = 0; for (var row = 0 ; row <4 ; row++) { newrow = document.createElement("tr"); for (var col = 0 ; col<4 ; col++) { newcol = document.createElement("td"); var button = document.createElement('input'); button.type = 'button'; button.width = '60'; button.height = '60'; button.id = (row*10 + col).toString(); newcol.appendChild(button); newrow.appendChild(newcol); if (row == 3 && col == 3) break; while (array[tmp = Math.floor((15)*Math.random()) + 1] == 1); array[tmp]++; button.value = tmp; } table.appendChild(newrow); } gameTable.appendChild(table); } function onClick(bid) { } window.onload=Init; </script> <body> <form id="myForm"> <div style=" color:Blue; font-size:large; font-style:oblique; left:500; top:30; position:absolute"> Fifteen Javascript </div> <div style="left:450; top:150; position:absolute""> <table id="gameTable"></table> </div> </form> </body> </html>
my question is how can i add an eventhandler for a button click for every button.
i want to add an onClick javascript function to be performed on every button click and send the buttons id as parameter.
i tried to add this line inside the loop:
newcol.children[0].onclick = onClick(newcol.children[0].id);
but it doesn't seems to work.
Last edited by emilio; Dec 17th, 2008 at 4:44 pm.
Here you have it...
Hope this will solve the issue...
Hope this will solve the issue...
javascript Syntax (Toggle Plain Text)
<html> <head> <title>javascript1</title> </head> <script lnaguage="javascript"> function Init() { var table = document.createElement("tbody"); var newrow,newcol,tmp; var array = new Array(16); for (var i=0 ; i<16 ; i++) array[i] = 0; for (var row = 0 ; row <4 ; row++) { newrow = document.createElement("tr"); for (var col = 0 ; col<4 ; col++) { newcol = document.createElement("td"); var button = document.createElement('input'); button.type = 'button'; button.width = '60'; button.height = '60'; button.id = (row*10 + col).toString(); newcol.appendChild(button); newrow.appendChild(newcol); if (row == 3 && col == 3) break; while (array[tmp = Math.floor((15)*Math.random()) + 1] == 1); array[tmp]++; button.value = tmp; } table.appendChild(newrow); } gameTable.appendChild(table); } document.onclick = function(e) { e = e ? e : window.event; t = e.target ? e.target : e.srcElement; if (document.getElementById && t.type == 'button') { ids = parseInt(t.id.match(/\d/)); /* You can add more function from the switch method. The only problem you have is that you've set your id's from the maximum count of "col" variable, which is 4 in overall count. So the first 4 line up of buttons will take this event. The rest will copy the function counting back from 0 to 4, which all the buttons from the 2nd row copy this effect! Things can be fixed by incrementing the col value = to the length of buttons being created by your function. Im sure it will be easy for you to achieve that. Good day to you... */ switch(ids) { case 0 : alert('This is function no#' + ids); break; case 1 : alert('Some function ' + ids + ' To Run'); break; case 2 : prompt('This is function no#' + ids); break; case 3 : confirm('This is function no#' + ids); break; } } } window.onload=Init; </script> <body> <form id="myForm"> <div style=" color:Blue; font-size:large; font-style:oblique; left:500; top:30; position:absolute"> Fifteen Javascript </div> <div style="left:450; top:150; position:absolute""> <table id="gameTable"></table> </div> </form> </body> </html>
Here, this should fix it:
EDIT: I don't know why you used an array by the way, and note the change I did with your switch case to see how it works with your buttons.
EDIT: I don't know why you used an array by the way, and note the change I did with your switch case to see how it works with your buttons.
html Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>javascript1</title> <script type="text/javascript"> function Init() { var table = document.createElement("tbody"); var newrow,newcol,tmp; var i = 0; for (var row = 0 ; row <4 ; row++) { newrow = document.createElement("tr"); for (var col = 0 ; col<4 ; col++) { i++; newcol = document.createElement("td"); var button = document.createElement('input'); button.type = 'button'; button.style.width = '60px'; button.style.height = '60px'; button.id = (row*10 + col).toString(); button.value = i; newcol.appendChild(button); newrow.appendChild(newcol); if (row == 3 && col == 3) { break; } } table.appendChild(newrow); } document.getElementById('gameTable').appendChild(table); document.onclick = function(e) { e = e ? e : window.event; t = e.target ? e.target : e.srcElement; if (document.getElementById && t.type == 'button') { ids = parseInt(t.id); /* You can add more function from the switch method. The only problem you have is that you've set your id's from the maximum count of "col" variable, which is 4 in overall count. So the first 4 line up of buttons will take this event. The rest will copy the function counting back from 0 to 4, which all the buttons from the 2nd row copy this effect! Things can be fixed by incrementing the col value = to the length of buttons being created by your function. Im sure it will be easy for you to achieve that. Good day to you... */ switch(ids) { case 0: alert('This is function no#' + ids); break; case 1: alert('Some function ' + ids + ' To Run'); break; case 2: prompt('This is function no#' + ids); break; case 3: confirm('This is function no#' + ids); break; case 10: alert('Hi, I\'m the button of the second row and the first column'); break; case 23: alert('Hi, I\'m the button of the third row and the last column'); break; default: alert('Other button'); } } } } </script> </head> <body onload="javascript:Init();"> <form id="myForm" action=""> <div style=" color:Blue; font-size:large; font-style:oblique; left:500px; top:30px; position:absolute"> Fifteen Javascript </div> <div style="left:450px; top:150px; position:absolute"> <table id="gameTable"></table> </div> </form> </body> </html>
Last edited by brechtjah; Dec 18th, 2008 at 8:13 am.
![]() |
Similar Threads
- Hidden Link - Please help. (PHP)
- Firefox Compatibility help with script (JavaScript / DHTML / AJAX)
- How do I identify what type and where my code is in the HTML DOM tree? (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: DIV content bigger than wrapper
- Next Thread: JQuery: Need Help Embedding Link in Accordion Menu
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor date debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post problem programming progressbar regex runtime scroll search security select shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n





