Connecting Tech Pros Worldwide Help | Site Map

Dynamically create a tag with onclick

  #1  
Old November 4th, 2005, 01:46 AM
shadow.demon@gmail.com
Guest
 
Posts: n/a
G'day guys,

i've looked around but i can't find how to create an A tag with a
onclick to remove a row in a table. Can add rows to table, but i can't
get it to create a link in the last coloumn to click to remove the row.

I've got:

var td6 = document.createElement("TD"); //create the TD
var link = document.createElement("a") //create element a
link.href = "test";
td6.appendChild(link); //add to the cell

the row function works fine but add a OnClick event to last coloumn i
can't seem to work out. Thanks guys.

  #2  
Old November 4th, 2005, 03:46 AM
RobG
Guest
 
Posts: n/a

re: Dynamically create a tag with onclick


shadow.demon@gmail.com wrote:[color=blue]
> G'day guys,
>
> i've looked around but i can't find how to create an A tag with a
> onclick to remove a row in a table. Can add rows to table, but i can't
> get it to create a link in the last coloumn to click to remove the row.
>
> I've got:
>
> var td6 = document.createElement("TD"); //create the TD
> var link = document.createElement("a") //create element a[/color]

Don't use an A element for this, use a button or style a text element to
look 'clickable':

var textButton = document.createElement('span');
textButton.className = 'clickable';
textButton.appendChild(document.createTextNode('De lete row'));
textButton.onclick = deleteParentRow;
td6.appendChild(textButton);

[color=blue]
> link.href = "test";
> td6.appendChild(link); //add to the cell
>
> the row function works fine but add a OnClick event to last coloumn i
> can't seem to work out. Thanks guys.
>[/color]

The deleteParentRow function should look like:

function deleteParentRow()
{
var el = this;
while(el.parentNode && 'tr' != el.nodeName.toLowerCase()){
el = el.parentNode
}
if ('tr' == el.nodeName.toLowerCase()) {
el.parentNode.removeChild(el);
}
}

The clickable style may be:

<style type="text/css">
.clickable {
cursor: pointer;
color: blue;
text-decoration: underline;
}
</style>


--
Rob
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically Setting Runat = Server on ButtonField? pbd22 answers 3 October 24th, 2007 07:35 PM
Dynamically create an anchor tag Andrew answers 1 December 13th, 2006 09:45 AM
ASP.NET 2.0 - Dynamically create ActiveX object using HtmlGenericControl and call methods / functions all from code behind qualitynice@hotmail.com answers 4 November 19th, 2005 10:53 PM
simple js dynamically reading form values - js fails - why? Randell D. answers 1 July 20th, 2005 01:06 PM