| | |
add onclick event programmatically
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2005
Posts: 3
Reputation:
Solved Threads: 0
I have a code that I call from Button_click event.
The code adds a new row to the table and attempts
to add onclick element to every td element in the new row.
Somehow, onclick does not work.
Any help is appreciated.
Here is the code:
function insRow()
{
var x = document.getElementById('Table1').insertRow(1);
for(i=0;i<4;i++)
{
var td = document.createElement('td');
td.innerHTML = 'NewItem';
td.setAttribute('onclick', 'alert("blabla")');
x.appendChild(td);
}
}
The code adds a new row to the table and attempts
to add onclick element to every td element in the new row.
Somehow, onclick does not work.
Any help is appreciated.
Here is the code:
function insRow()
{
var x = document.getElementById('Table1').insertRow(1);
for(i=0;i<4;i++)
{
var td = document.createElement('td');
td.innerHTML = 'NewItem';
td.setAttribute('onclick', 'alert("blabla")');
x.appendChild(td);
}
}
•
•
Join Date: May 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by alpha_foobar
hey,
your code works in firefox. so i'd say it pretty close to working in IE. the only change i had to make was in regards to the table row index.. but i presume you already have a row in your table.
I don't know what is wrong with the code, it just doesn't work...
the function attachEvent is IE specific, so if this code still doesn't work, this is a possible solution for IE browsers. And could be used as follows:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
if( td.attachEvent ){ td.attachEvent('onclick', 'alert("blabla")'); } else { td.setAttribute('onclick', 'alert("blabla")'); }
•
•
Join Date: May 2005
Posts: 2
Reputation:
Solved Threads: 0
actually, i think you hand in a handle to the function (not a string) and I don't think you can hand in parameters into the function. Thus, check this out to help understand how you can still pass in parameters to an onclick.
function myOnclick() {
sendingObj = event.srcElement;
alert (sendingObj.myProperty);
}
...
img.myProperty = "blahblah";
img.attachEvent("onclick", myOnclick); //notice, no brackets... no parametes, no quotes
function myOnclick() {
sendingObj = event.srcElement;
alert (sendingObj.myProperty);
}
...
img.myProperty = "blahblah";
img.attachEvent("onclick", myOnclick); //notice, no brackets... no parametes, no quotes
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Need to Pass Variables from form to form
- Next Thread: Draggable boxes
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column cookies createrange() css cursor debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms 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 session shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n





