querySelectorAll doesn't work?


I stock up on tea

Here is my javascript code:

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

function talk(){
alert('hello!');
}

window.onload = answer();

When running, it pops up a browser window with a warning saying "hello". my html code:

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

It will give a warning when the tab is loaded. And when I click on the tuna I want it to run with a warning!

Damien_The_Unbeliever

You are not assigning the event handler correctly - you are assigning the result of calling the function , not the function itself. remove ()s:

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

function talk(){
alert('hello!');
}

window.onload = answer;

What is currently happening is that the function window.onload = answer();can be run as soon as a row is clicked . In turn, when it reaches the line , it will call the function immediately. That's not what you want.answeronclicktalk

Related


querySelectorAll doesn't work

username I have a requirement where I have .divto pick the last container in the container and apply some business logic to it. The selection of the last option .divmust be dynamic, as the user can choose to add/remove .divelements. I tried it initially, query

document.querySelectorAll ":not" with condition doesn't work

wsddd_ I tried to replace jQuery 's functionality .not()natively , but unfortunately it document.querySelectorAlldoesn't work for my use case. This is what I want to achieve - change the jQuery selector to vanilla JS: $('#someID').not('.classNameOne > div, .cl

querySelectorAll.style doesn't work

computer backup I'm writing something in JavaScript that I need to use, querySelectorAll.stylebut it always returns undefined, but works perfectly with querySelector.style. How can I make it work so I can style it? document.querySelector("div#tabs" + tabId + "

QuerySelectorAll(input[type=select]) doesn't work

Kirby L. Wallace I can select all other types of elements on the page except: var elems = querySelectorAll("input[type=select]"); Once I have them, I .disabledapply in a loop: for (var i = 0; i < elems.length; i++) { elems[i].disabled = true; } This appl

document.querySelectorAll ":not" with condition doesn't work

wsddd_ I tried to replace jQuery 's functionality .not()natively , but unfortunately it document.querySelectorAlldoesn't work for my use case. This is what I want to achieve - change the jQuery selector to vanilla JS: $('#someID').not('.classNameOne > div, .cl

QuerySelectorAll(input[type=select]) doesn't work

Kirby L. Wallace I can select all other types of elements on the page except: var elems = querySelectorAll("input[type=select]"); Once I have them, I .disabledapply in a loop: for (var i = 0; i < elems.length; i++) { elems[i].disabled = true; } This appl

querySelectorAll doesn't work in IE8?

test I'm talking about this is the ie8 related gallery : http://tympanus.net/codrops/2014/03/21/google-grid-gallery/ This is the line where js breaks: var x = this.grid.querySelectorAll( 'li:not(.grid-sizer)' ); But I think the problem started early. When I t

QuerySelectorAll doesn't work as expected on img alt

Kennedy This is the line of code I'm trying to target:<img class="pi" src="/graphics/ni/ni90.jpg" alt="Awaiting image"> I am trying to select all images with that propertyalt="Awaiting image" if i trydocument.querySelectorAll("img.pi"); I will get all images w

querySelectorAll.style doesn't work

computer backup I'm writing something in JavaScript that I need to use, querySelectorAll.stylebut it always returns undefined, but works perfectly with querySelector.style. How can I make it work so I can style it? document.querySelector("div#tabs" + tabId + "

querySelectorAll.style doesn't work

computer backup I'm writing something in JavaScript that I need to use, querySelectorAll.stylebut it always returns undefined, but works perfectly with querySelector.style. How can I make it work so I can style it? document.querySelector("div#tabs" + tabId + "

Why doesn't QuerySelectorAll work on my modal page

Kevin I'm pretty new to coding and trying to teach myself through books and other resources available, so please don't bother me. Does anyone know how to make my close button work for all modal pages that use querySelectoror QuerySelectorAll? Currently, I'm tr

querySelector works but querySelectorAll doesn't

username I am new to JS. There are 3 svg's and I'm trying to change the fill color on mouseover. The first one works perfectly when I use querySelector('.svg'). I want to change all three parameters, not only the first one, but also none of querySelectorAll('.

querySelector works but querySelectorAll doesn't

username I am new to JS. There are 3 svg's and I'm trying to change the fill color on mouseover. The first one works perfectly when I use querySelector('.svg'). I want to change all three parameters, not only the first one, but also none of querySelectorAll('.

querySelector works but querySelectorAll doesn't

username I am new to JS. There are 3 svg's and I'm trying to change the fill color on mouseover. The first one works perfectly when I use querySelector('.svg'). I want to change all three parameters, not only the first one, but also none of querySelectorAll('.