Create a simple button using Javascript

Well, humaira, what you’re trying to do is pretty much advanced… and you seem to need to learn some basic stuff first. I’d suggest you try finding some reading / examples / tutorials in google or in your bookstore. Good luck!

thanks for ur help guys much appreciated…

All the ways of sending arguments to a function are roughly equivalent. The easiest way to send an argument to a function is to just declare the argument as a global variable and use it inside the function. Try this simple example:

var interation = 10;

function my_func()
{
    alert(interation);
}

my_func();

Also, get rid of any line with attachEvent() and get rid of any setAttribute() line that tries to set an onclick event because they are both not crossbrowser. To set an event handler all you need is a line like this:

buttonnode.onclick = some_function;

It’s been a year, but I found this tread via Google search when looking into attachEvent with the error ‘type mismatch’. I had (operative word-- “had”) a similar problem.

I wanted to do something like


var iteration = 0;
. . .

function addRow() {

iteration++;
. . .

var cell4 = row.insertCell(4);
var print = document.createElement('input');
print.setAttribute('id', PRINT_PREFIX + iteration);
print.setAttribute('name', PRINT_PREFIX + iteration);
print.setAttribute('type', 'button');
print.setAttribute('value', 'Print');
[COLOR=Red]print.attachEvent('onclick', printBoxLabels(this.parentElement));[/COLOR]
cell4.appendChild(print);
}

where printBoxLabels() printed a label using data from the row in question and replaces the print button with text (via InnerHTML).

What I discovered is IE’s (at least in IE 6) attachEvent only accepts the function name as the second argument additionally executing said function immediately. Furthermore, the event, itself, is passed as the first argument to the function whenever the event is fired.

So just use arguments[0].srcElement to get the referring element in the function you want to call (in my case: printBoxLabels). . . and through an appropriate number of [element].parentElement you can get from the input element to the td element to the tr element.

To keep the function from firing upon the attachEvent call, I use an assertion at the beginning of printBoxLabels(), that simply returns to addRow() upon failure.

I hope this helps some one out there.