ASM wrote:
Daz a écrit :
Hello everyone. I am sure the answer to my question is simple, but I
can't seem to dynamically add an onClick event to my script.
I have a table which is generated dynamically, I am just struggling
getting an onClick event to show in the HTML source.
Any help would be appreciated. Here is a block of my current code which
doesn't work.
var tr1 = document.create Element('tr');
tr1.style.backg roundColor = "blue";
var td1 = document.create Element('td');
var titleSpan = document.create Element('span') ;
titleSpan.style .color = "white";
titleSpan.style .fontWeight = "bold";
titleSpan.onCli ck = function() {this.toggle(); };
titleSpan.inner HTML = menuTitle;
td1.appendChild (titleSpan);
tr1.appendChild (td1);
tbody.appendChi ld(tr1);
use tbody instead of table
.
Thanks ASM. my table variable is actually a reference to tbody. You're
right, though. I should have named it tbody and not table.
I still can't get it to work. For some reason, the toggle function is
not working at all. I have tried adding the function to the onClick
event directly, and it still doesn't work... I don't even get an
error/warning in my JavaScript Console...
I know it's something I am doing wrong, I just can't figure out what.
Here is my code. It looks messy because I am trying to figure out how
things work...
//********** CODE START **********
var table = document.getEle mentById('ctg_t able');
var tableWidth = 80;
function initPage()
{
table.parentNod e.style.width = tableWidth + "%";
displayInputDia log();
var row1 = new menuItem("test" );
}
function displayInputDia log()
{
var tr = document.create Element('tr');
tr.setAttribute ("id", "rInputDialog") ;
tr.style.height = "20%";
var td = document.create Element('td');
textarea = document.create Element('textar ea');
textarea.setAtt ribute("name", "inputDialo g");
textarea.setAtt ribute("id", "inputDialo g");
textarea.style. width = "100%";
textarea.style. height = "100%";
textarea.style. borderStyle = "ridge";
table.appendChi ld(tr);
tr.appendChild( td);
td.appendChild( textarea);
}
function menuItem (menuTitle)
{
var tr1 = document.create Element('tr');
tr1.style.backg roundColor = "blue";
var td1 = document.create Element('td');
var b = document.create Element('b');
var titleSpan = document.create Element('span') ;
titleSpan.style .color = "white";
table.appendChi ld(tr1);
tr1.appendChild (td1);
td1.appendChild (b);
b.appendChild(t itleSpan);
titleSpan.inner HTML = menuTitle;
var tr2 = document.create Element('tr');
var td2 = document.create Element('td');
table.appendChi ld(tr2);
tr2.appendChild (td2);
tr2.style.borde rStyle = 'ridge';
tr2.style.borde rColor = "green";
td2.innerHTML = 'blah';
titleSpan.onCli ck = function()
{
var textarea = document.getEle mentById('input Dialog');
textarea.value = "blah"
}
}
initPage();
//************ CODE END ************
Many thanks for your input.
If you can offer any more constructive criticism, it would be received
gracefully as I would rather do this right, than go about it the wrong
way.
Daz.