Sofiia
Sofiia

Reputation: 59

Add node to the div

I want to add new node to the div on click on the button. Operator this must be as the parameter of the function addNode(this)

<div id = "files">
    <div>
        <input type="file" name="main_photo">
        <input type="button" value="add" onclick="addNode(this)">
        <input type="button" value="delete" onclick="deleteNode(this)">
    </div>
</div>
<script language="javascript">
    function addNode(this){
        var x = document.getElementById("files");
        var y = x.getElementsByTagName("div");
        var clone = y.cloneNode(true);
        x.appendChild(clone);
    };

</script>

Upvotes: 0

Views: 83

Answers (1)

Amit Joki
Amit Joki

Reputation: 59232

You need to have a different name when declaring the parameter, since this is a keyword.

function addNode(elem){
    var x = document.getElementById("files");
    var y = x.getElementsByTagName("div");
    var clone = y.cloneNode(true);
    x.appendChild(clone);
};

Also, y is a NodeList and you need to access the elements in it by indexes which you ain't doing. Therefore var clone = y[0].cloneNode(true)

BTW, you're not using that parameter, are you?

Upvotes: 2

Related Questions