Jquery: add button to cell

How can I insert a button into a table cell using jquery and handle the onclick event for that button?

Thank.

+3
source share
2 answers
<script>
    function insert(button){
        $(button).appendTo($('td.toinsert'));
    }
</script>
<table>
    <tr>
    <td class="toinsert">inside cell</td>
    </tr>
</table>
<input type="button" onclick="insert(this)" value="button"/>

Also to create a button and add an event to it

<script>
    $(function(){
        $('<input></input>').attr({'type': 'button'}).val("button").click(function(){
            alert('hello');
        }).appendTo($('td.toinsert'));
    });
</script>
<table>
    <tr>
    <td class="toinsert">inside cell</td>
    </tr>
</table>
0
source
var button = $("<button>Hi there</button>");
button.click(function() {
    alert("Hi back!");
});
button.appendTo("#tablecell");

http://jsfiddle.net/Fxayf/1/

+4
source

Source: https://habr.com/ru/post/1793558/


All Articles