whats wrong with my code??

0 varun teja · September 30, 2015

function answer(){
    var list = document.querySelectorAll("#fish");
    list[1].onclick = talk();

function talk(){

window.onload = answer();

on run it pops my browser window with alert: 'hello'.
my html code:

<!DOCTYPE html>
    <title>my site</title>
    <script src="new 3x.js"></script>
            <p id="fish">hello world!</P>
            <p id="fish">tuna</P>
            <p id="stuff">durr</P>

it alerts hello on loading the tab.whereas i want it to run alert when i click tuna!
by the way iam learning the tutorial HTML5 Tutorial - 26 - querySelectorAll.

Post a Reply


Oldest  Newest  Rating
0 Bram Dekker · October 1, 2015
Could you try the following? 
list[1].onclick = talk;

the parentheses behind talk in your code indicate that you execute it directly instead of just assigning the function to onclick.
0 varun teja · October 2, 2015
s it woked for me thnx!
  • 1

HTML / CSS / Web Design


Discuss, share, ask, learn and teach HTML5 and CSS3.