•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 373,915 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,693 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 33111 | Replies: 10
![]() |
•
•
Join Date: May 2005
Posts: 3
Reputation:
Rep Power: 0
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
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 3
•
•
Join Date: May 2005
Posts: 3
Reputation:
Rep Power: 0
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.
The first line of my code adds a new row to the table.
I don't know what is wrong with the code, it just doesn't work...
•
•
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 3
•
•
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 3
•
•
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation:
Rep Power: 4
Solved Threads: 3
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:
if( td.attachEvent ){
td.attachEvent('onclick', 'alert("blabla")');
} else {
td.setAttribute('onclick', 'alert("blabla")');
}•
•
Join Date: May 2005
Posts: 2
Reputation:
Rep Power: 0
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Previous Thread: Need to Pass Variables from form to form
- Next Thread: Draggable boxes


Linear Mode