0

This is a page witch i am about to make it upload a image but when i select the element by id in javascript the browser say 'Cannot set proprety "onchange" of null' But i wrote the exact same id please help this is my code

  <!DOCTYPE html>
  <html>
  <head>
      <title>Register</title>
      <link href='http://fonts.googleapis.com/css?family=Arvo:400,700|Ubuntu:300,400,500,700' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="/css/style.css"/>
    <script>
        function _(id){
            return document.getElementById(id);
        }
        _("file_upload").onchange = function() {
            alert("It works!");            };

    </script>
</head>
<body>
    <div id="container">
        <div id="half1">
            <div id="upload">
               <div id="user">
                   <img src="/img/1.png" alt="User image"/>
               </div>
              <img src="/img/plus.png" alt="Upload" />
              <input type="file" id="file_upload"/>
            </div>
        </div>
    </div>
</body>

Maroxtn
  • 601
  • 4
  • 7
  • 15
  • Note: this is not Duplicate because my case is not the exact same as the others – Maroxtn Dec 22 '14 at 14:36
  • 3
    "this is not Duplicate because my case is not the exact same as the others" — wrong on both counts. – Quentin Dec 22 '14 at 14:37
  • The `` tag has not been loaded nor it's contents therefore the javascript executes before the element with ID file_upload exists in the DOM. Place the javascript just before the close `` tag. But yes this is a duplicate. The referenced post is the exact same scenario. – brenjt Dec 22 '14 at 14:41
  • Thanks i didn't found that one this really work ! – Maroxtn Dec 22 '14 at 15:09

0 Answers0