javascript - Why does this event listener not work? -


i tried adding event listener cars3 id'd paragraph when click paragraph should execute getvalue() method not work. don't understand why. thanks.

<!doctype html> <html lang="en">  <head>  <script>      function getvalue()    {         var x=document.getelementbyid("cars").value;          document.getelementbyid("cars2").innerhtml = x.tostring();     }  document.getelementbyid("cars3").addeventlistener("click", getvalue());  </script>  </head>  <body>  <h1 id="myheader" onclick="getvalue()">"click here !" </h1>  <p id="cars3">afaf</p>   <select id="cars" name ="cars">       <option value="volvo">volvo</option>       <option value="saab">saab</option>       <option value="mercedes">mercedes</option>       <option value="audi">audi</option></select>    </select>   <p id="cars2">afaf2</p>  </body>  </html> 

remove brackets:

addeventlistener("click", getvalue()); //--------------------------------^^ 

making it:

addeventlistener("click", getvalue); 

edited

also move script tag below body. code not waiting page rendered, hence document.getelementbyid("cars3") fails fetch element.

<body>   ... </body> <script> function getvalue()   ... <script> 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -