1

I have the following javascript function which creates a new row with different cells including button as the cell item type. So how can I implement onClick of the button functionality here?

My Code:

var propertyCell = row.insertCell(1);  
    var propertyName = document.createElement("input");  
    propertyName.type = "text";  
    propertyName.size = "6";  
    propertyName.id = "pName"+rowCount;  

    //alert(propertyName.size);  
    propertyCell.appendChild(propertyName);  
var image1Cell = row.insertCell(6);  
    var image1 = document.createElement("input");  
    image1.type = "button";  
    image1.value = "Camera";  
    image1.size = "6";  
    image1.id = "image1"+rowCount;  
    //image1.onclick = function(){sdfkvk(dh,sdd)}  

    image1Cell.appendChild(image1);
0

2 Answers 2

2

You may try the following code:

image1.onclick = function (){alert(document.getElementById("pName"+rowCount).value)};
Sign up to request clarification or add additional context in comments.

Comments

0

You get the element of the textfield using document.getElementById as the rowcount is the same as the button's value you can get the number by doing a string split. Then I just alert the value, but once you have the value you can do what you like.

  image1.onclick = function () { 
      alert(document.getElementById("pName" + this.id.split('e1')[1]).value); 
  };

1 Comment

this.id.split('e1')[1] should return the rowcount number from image1.id, as {this} would point to the button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.