tags:

views:

54

answers:

3
function main(){

var delImage=document.createElement("img"); 
delImage.setAttribute("alt","Edit"); 
delImage.setAttribute("src","drop.png");

var position=newRow.rowIndex;
var typeElem=document.createElement("a");
typeElem.setAttribute("name","delete");  
typeElem.setAttribute("href","#");
typeElem.appendChild(delImage);
typeElem.setAttribute('onclick',"delete(position)");

newRow.insertCell(lastCell).appendChild(typeElem);

}

function delete(pos){
alert(pos);
}

i am not able to call the delete function when anchor tag was clicked...what can i want to change to achieve this?

A: 

Try

typeElem.onclick = function(){
    delete(position);
};

and better use a more meaningful name like deletePosition or something like that

rahul
A: 

Try changing:

typeElem.setAttribute('onclick',"delete(position)");

to

typeElem.setAttribute('onclick',"delete(" + position + ")");
Tom Gullen
thanks tom and rahul...Tom's answer was perfectly match to my recquirement
Raam
why can we pass a value like this instead of basic way?if i want to pass a value like this means /* position=1+"raam"; */(postion=1raam)..how can ido this?thanks.
Raam
We are not passing a value as such, we are taking the literal value of the variable and assigning it to the attribute function call, if that makes sense. Hard to explain, maybe someone else can do it better.
Tom Gullen
A: 

IE handles setAttribute differently to other browsers, particularly event handlers attributes. For this reason and for the sake of neater code it's much easier to avoid using setAttribute where DOM properties already exist, which generally work uniformly across browsers. Also, avoid using functions named after operators in JavaScript. Specifically in this case I'd rename the delete() function.

function main() {
    var delImage = document.createElement("img");
    delImage.alt = "Edit";
    delImage.src = "drop.png";

    var position = newRow.rowIndex;
    var typeElem = document.createElement("a");
    typeElem.name = "delete";
    typeElem.href = "#";
    typeElem.appendChild(delImage);
    typeElem.onclick = function() {
        doDelete(position);
    };
    newRow.insertCell(lastCell).appendChild(typeElem);
    typeElem = null;
}

function doDelete(pos){
    alert(pos);
}
Tim Down