2

I want to insert new HTML Input Tag in td for Text.My Leader said me not to use jquery and other plugins so, I think that can be only Javascript.I want to insert new Input tag for all td in table. How can I insert New HTML Input Tag??? Pls Help Me !

<script type="text/javascript">
var btn_name=document.getElementsByName("submitConfirm");
function init()
{
    document.getElementsByName("submitConfirm").onClick=function()
    {
    Edit_td();
    }
}
function Edit_td()
{

}

window.onload(init());

That's my prepared code.

5
  • show your markup also? and I didn't get what do you mean by only element name please specify. Commented Jul 16, 2014 at 2:34
  • "submitConfirm" is name of Input.I can handle that but I don't know how to insert Input tag within td when click the button Commented Jul 16, 2014 at 2:47
  • when you function init is getting called ? Commented Jul 16, 2014 at 3:08
  • Sorry I forgot that, I call that from window.onload Commented Jul 16, 2014 at 3:34
  • I've updated my answer for your problem. try that. Commented Jul 16, 2014 at 3:48

2 Answers 2

1

I tried this, should work for you :-

function editTd() {
    var rows = document.getElementById('tbl1').rows;
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        for (var j = 0; j < row.cells.length; j++) {
            var input = document.createElement('input');
            input.type = "text";
            row.cells[j].appendChild(input);
        };
    };
}

window.onload = function(){
    document.getElementsByName("submitConfirm")[0].onclick = editTd;
};

This is my markup

<button name="submitConfirm">submit</button>
<table id="tbl1">
  <tr>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
  </tr>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1
var td = document.querySelector('td') // or getElementById/ClassName[0]  

var input = document.createElement('input')
input.type = 'text' // or other

td.appendchild(input)

5 Comments

In my Web page,that has other td from other table so that will be affect all, isn't it?how can I take td from my target table.I can only use table ID...
put here your html, without that i cant help you
you can do var table = document.getElementById('table-id') var td = table.querySelector('td')
I can't touch html too.He give me table id other tag name.I can run to see output.I can see only tags from Mozilla Inspector . That's all. I'm sorry
That will be great :D

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.