views:

173

answers:

3

What's wrong with my code?

<script>
    var UrlNumber;

    function addMoreUrl()
    {
        if(document.getElementById("accept").checked == false)
        {
            alert("Please acceept");
            return false;
        }

        var div   = document.createElement("div");
        var link  = document.createElement("a");
        var url   = document.createElement("input");
        var send  = document.createElement("input");
        url.setAttribute("type", "text");
        url.setAttribute("id", 'url'+UrlNumber);
        url.setAttribute("size", "60");
        url.setAttribute("name", 'url'+UrlNumber);
        //url.setAttribute("onchange", alert("url todo"));
        link.setAttribute("href", "javascript:removeUrl('url"+UrlNumber+"');");
        link.appendChild(document.createTextNode("Entfernen"));
        div.setAttribute("id", "url"+UrlNumber);
        send.setAttribute("type", "checkbox");
        send.setAttribute("id", 'check'+UrlNumber);
        div.appendChild(url);
        div.appendChild(link);
        div.appendChild(send);
        document.getElementById("moreUrl").appendChild(div);

        if(document.getElementById('check'+UrlNumber).checked == true)
        {
           alert("checked");
        }
        UrlNumber++;
    }

    function removeUrl(urlElement)
    {
        var element = document.getElementById(urlElement);
        document.getElementById("moreUrl").removeChild(element);
        UrlNumber--;
    }

    </script>
+2  A: 

UrlNumber never gets initialised?

var UrlNumber = 0;

Just a guess though as you haven't told us what's actually happening...

Mark B
Yeah look at my comment i fixed that but what about the checkbox?
streetparade
What checkbox? You didn't post any HTML.
Mark B
+2  A: 

Try initializing UrlNumber as follows:

var UrlNumber = 0;

Because UrlNumber would become an NaN in the following case:

var UrlNumber;
UrlNumber++;
Daniel Vassallo
Thanks i fixed that, what about the checkbox?
streetparade
What checkbox? You didn't post any HTML. (c) Mark B
Natrium
+4  A: 

Firstly, I'd suggest using a framework such as jQuery to abstract all of the DOM traversal/manipulation away; this will allow you to write much tidier and more concise code.

Secondly, the reason nothing is happening when the checkbox is checked is that you're only testing it immediately after you've created it. What you want is to use the onchange event to check every time the field's value changes:

document.getElementById('check'+UrlNumber).onchange = function()
{
    if (this.checked)
    {
        alert("checked");
    }
}

Thirdly, try to make your questions less vague in the future. Explain the problem as clearly as possible in your question and provide only the relevant bits of code.

Will Vousden
please, do not *try* to be less vague, **be less vague**!
Natrium
Thanks For your help this fixed the problem, i will do what you said. Have a nice Day
streetparade
+1 for finding the bug!
Daniel Vassallo