Create a simple button using Javascript

And by the way I just tested it under FF1 it works !

It doesn’t work for me, and it hasn’t worked for me in the past. I’m using FF1.0 on windows. How about you? Here’s my code:


<html>
<head><title></title>
<script type="text/javascript" language="javascript">
<!-- Hide from browsers without javascript

function g()
{
	alert("you clicked?");
	
}

window.onload=function()
{
	var buttonnode= document.createElement('input');
	buttonnode.setAttribute('type','button');
	buttonnode.setAttribute('name','button'+3);
	buttonnode.setAttribute('value','sal');
	buttonnode.setAttribute('onclick', function(){alert('Clicked')});
	document.getElementsByTagName("body").item(0).appendChild(buttonnode);
};

// End hiding -->
</script>
</head>
<body>

<div>some text</div>

</body>
</html>

[QUOTE=7stud]Yes…and you can always try it. :slight_smile: In js, you can add properties to any object at will. For instance, you can do this:



ok well according to the above suggestion I have tried the following but I get a type mismatch error

function removerow(a){
document.getElementById('secondtable').deleteRow(a);
addRowToTable();
}

function addRowToTable()
{
	var tbl = document.getElementById('secondtable');
	var lastRow = tbl.rows.length;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	
           var row = tbl.insertRow(lastRow);
	// left cell
	var cell1 = row.insertCell(0);	
	var cell2=row.insertCell(1);
	var cell3=row.insertCell();
    	var cell4=row.insertCell();

	
	var e1= document.createElement('input');
	e1.setAttribute('type','text');
	e1.setAttribute('name','field'+4);
	cell2.appendChild(e1);
		
	var buttonnode= document.createElement('input');
	buttonnode.setAttribute('type','button');
	buttonnode.setAttribute('name','button'+iteration);
	buttonnode.setAttribute('value','sal');
	removerow.a=iteration;
	buttonnode.attachEvent('onclick',removerow.a);//require this particular row to be deleted
	cell1.appendChild(buttonnode);
   	

}


this is what I have added in:

             removerow.a=iteration;
	buttonnode.attachEvent('onclick',removerow.a);//require this

Huh? The second parameter of attachEvent must be a function reference, which is a function name without the function execution operator: (). That means the variable:

removerow.a

has to be a function reference. This is what you did:

removerow.a = iteration = lastRow = tbl.rows.length

First, you could dispense with all that and just do this:

removerow.a = tbl.rows.length;

However, tbl.rows.length is an integer value and not a function reference. I answered a question by JRMillion that really had nothing to do with your script, so I don’t know why you decided to modify your code in that way.

ok I am not quite understanding this what i am trying to do is send the iteration value from the the function into the remove row function

example

var a= iteration

and i want to send a into the remove row function

buttonnode.attachEvent(‘onclick’,removerow)

If you didn’t like the solutions I proposed in post #14, and you want to get tricky and apply that object stuff I was discussing with JRMillion in order to pass an argument to the function, you could do this:

removerow.a = interation;

buttonnode.attachEvent(‘onclick’,removerow)

Then in the removerow() function, you could do this:

function removerow()
{
    alert(removerow.a);
}

But once again, attachEvent() is not crossbrowser, and it’s so much easier to type:

buttonnode.onclick = removerow;

which works in ALL modern browsers.

I understand this is a parameter issue and that they cannot be sent over

for a little example I have done this:

var display;
function Hi()
{
alert(display);
}

var buttonnode= document.createElement(‘input’);
buttonnode.setAttribute(‘type’,‘button’);
buttonnode.setAttribute(‘name’,‘button’+iteration);
buttonnode.setAttribute(‘value’,‘sal’);
//removerow.a =iteration;
buttonnode.attachEvent(‘onclick’,Hi);//require this particular row to be deleted
buttonnode.onclick = function(){Hi(this, iteration)};
cell1.appendChild(buttonnode);

but when I click on the button and the alert jus outputs undefined and not the value of iteration…

Here’s the code I tested under FireFox 1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>IE Bug ?</title>
  <script>

  function showit(){
  alert('ok')
  }

  // Cherche l'élément 'bug' et insère un lien qui affiche 'ok' quand on clique
  // Fonctionne sous Mozilla et Opera mais pas sous IE 6
  function testIE(){
    var elem = document.getElementById("bug");
    var newtag = document.createElement("a");
    newtag.setAttribute("id","lien")
    newtag.setAttribute("onclick",function(){alert('coucou')})
    newtag.onclick=function(){alert('coucou')};
    newtag.setAttribute("style","cursor:pointer")
    newtag.appendChild(document.createTextNode("click"));
    elem.appendChild(newtag);

    document.getElementById('lien').style.cursor='hand, pointer'

  }
  </script>
  </head>


  <body>

  <input type="button" value="test!" OnClick="testIE()"/>

  <div id="bug"></div>

  </body>
</html>  

onclik attribution is doubled to ensure compatibility with IE and FFX

onclik attribution is doubled to ensure compatibility with IE and FFX

Hmmm…let me review: you claimed setAttribute() worked in FF1.0 with event handlers:

newtag.setAttribute(“onclick”,function(){alert(‘coucou’)});

but your code really has another line of code that assigns the function to the the onclick event in FF1.0:

newtag.onclick=function(){alert(‘coucou’)};

Furthermore, since the last line works in IE too, you don’t need the first line at all.

i’m confused…

but when I click on the button and the alert jus outputs undefined and not the value of iteration…

Where did you define interation anywhere in your example:


var display;
function Hi()
{
alert(display);
}


var buttonnode= document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('name','button'+iteration);
buttonnode.setAttribute('value','sal');
//removerow.a =iteration;
buttonnode.attachEvent('onclick',Hi);//require this particular row to be deleted
buttonnode.onclick = function(){Hi(this, iteration)};
cell1.appendChild(buttonnode);

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;

Finally, since you seem to be having a lot of trouble with that concept, I suggest you put your event handlers inline in the html:

<input type=“button” onclick=“some_function(arg1)” />

and see if that makes things clearer for you.

ok think I have come up with the solution of sending the parameter across but the thing is now it just removes the last row and not the button row any suggestions as to how I can retrieve the row of the button I have created…as the row is added at the end of the table I assumed that its index would be last row but this does not seem to be the case

function remove1()
{
var num=remove1.a;
document.getElementById(‘secondtable’).deleteRow(num);
}

       	var tbl = document.getElementById('secondtable');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;


var buttonnode= document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('name','button'+iteration);
buttonnode.setAttribute('value','sal');
remove1.a =iteration;
buttonnode.attachEvent('onclick',remove1);//require this particular row to be deleted

Array index values start at 0, so if you have an array with length 3 the index values are:

0, 1, 2

Notice there is no index value of 3 = array.length. In an array, there can never bee anything at position array.length in the array.

This is because your “remove1.a” value gets overwritten by this line


 remove1.a =iteration;

and this code


var num=remove1.a;

uses the last value of “remove1.a”.

This is the way how closures work, and you have to create extra scope to work around this. The good news are that there is actually no need to pass “iteration” to event hanlder, because you can use row’s “rowIndex” property instead.

The example is an old code I digged up from my library, probably in more recent version of browsers last line is sufficient …
For setAttrib I did not claim anything I just rememberedr I havd a code somewhere that got me out of a similar problem I encountererd e few years ago …

hi once again…
erm I have had a look at the above site but I dont see them using rowIndex within their example:

function formatTable(oTable)
{
var rows=oTable.rows;
for(var i=0;i<rows.length;i++)
{
if(i%2==0) {
rows[i].style.backgroundColor = “black”;
rows[i].style.color = “white”;
} else {
rows[i].style.backgroundColor = “white”;
rows[i].style.color = “black”;
}
}

just to clarify the matter i have a button associated with each row when clicking upon the button it should delete the corresponding row. I would appreciate if you could demonstrate how I would use rowIndex to find the index to the row…

Actually you dont even need rowIndex. Just an element itself.


<table border=1>
	<tr><td>1</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>2</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>3</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>4</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>5</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>6</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>7</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>8</td><td><button onclick="deleteRow(this)">x</button></td></tr>
	<tr><td>9</td><td><button onclick="deleteRow(this)">x</button></td></tr>
</table>
<script language="JavaScript" type="text/javascript">
function deleteRow(o) {
	while(o && o.tagName != "TR")
		o = o.parentNode;
	if(o)
		o.parentNode.removeChild(o);
}
</script>

but I am actually creating the row in javascript then trying to delete the row:

here is the code that I have tried but I think this.rowIndex is just deleting the first row:

function remove1()
{
var num=remove1.a;

var mytable= document.getElementById(‘secondtable’);
document.getElementById(‘secondtable’).deleteRow(this.rowIndex);
}

function addRowToTable()
{
var tbl = document.getElementById(‘secondtable’);
var lastRow = tbl.rows.length;
// if there’s no header row in the table, then iteration = lastRow + 1
var iteration = lastRow-1;
alert(iteration);
var row = tbl.insertRow(lastRow);
// left cell
var cell1 = row.insertCell(0);
alert(lastRow +“hello”);
var cell2=row.insertCell(1);
var cell3=row.insertCell();
var cell4=row.insertCell();

var e1= document.createElement('input');
e1.setAttribute('type','text');
e1.setAttribute('name','field'+4);
cell2.appendChild(e1);
	
var buttonnode= document.createElement('input');
buttonnode.setAttribute('type','button');
buttonnode.setAttribute('name','button'+iteration);
buttonnode.setAttribute('value','sal');
remove1.a =iteration;
buttonnode.attachEvent('onclick',remove1);//require this particular row to be deleted

	//buttonnode.attachEvent('onclick',removerow);//require this particular row to be deleted
//buttonnode.onclick = function(){Hi(this, iteration)};
cell1.appendChild(buttonnode);
//buttonnode.onclick = Hi;

}

buttonnode.attachEvent(‘onclick’,remove1);//require this particular row to be deleted

because of the above line I am unable to add parameters to the as it does not allow to do so