0

I have added the below HTML and JavaScript code in an HTML file and loaded it in a UIWebView. On tapping the button "Test", no alert is shown. Actually there is no element in the HTML document with id "mydiv". What is wrong with the code ? or can't I use document.getElementById in UIWebView ?

<input type="button" value="Test" onclick="getElement()"/>

function getElement()
{
    var mydiv = document.getElementById("mydiv");
    if(typeof (mydiv) == 'undefined')
    {
        alert(typeof (mydiv));
    }
    mydiv = document.createElement("div");
    mydiv.style.width = 100+"px";
    mydiv.style.height = 200+"px";
    mydiv.id = "mydiv";
    document.body.appendChild(mydiv);
}

1 Answers1

0

instead of checking is undefined
change your code to

 if(!mydiv)
    {
        alert(typeof (mydiv));
    }

this will alert if mydiv dosent exist
jsfiddle for you code

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
shaN
  • 798
  • 12
  • 23
  • Checked.but same result. Checked on Mac. –  Nov 20 '14 at 10:07
  • alert is shown if there is no mydiv, change `if(!mydiv)` to `if(mydiv)` if you want alert to be shown if mydiv exists – shaN Nov 20 '14 at 10:13