W3Schools jQuery Quiz

There's a jQuery poll posted on W3Schools ...

http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery

Question No. 19 is as follows:

Take a look at the following jQuery selector: $ ("div # intro.head").

What does he choose?

but. The first element with id = "head" inside any div element with class = "intro"

B. All elements with class = "head" inside the first div element with id = "intro"

C. All div elements with id = "intro" or class = "head"

I understood this correctly by choosing answer B.

My question is about the wording of answer B.

Should the word "first" be removed from the answer?

B. All elements with class = "head" inside a div element with id = "intro"

The identifier is defined as “a unique identifier for an element”, so it’s not entirely clear why they will refer to “element first div with id = intro”

I do not believe that this is intentionally trying to be difficult, since all the other questions in this quiz are very simple.

Thanks for your thoughts.


EDIT:

I reported this error to W3Schools and directed them to this thread.


EDIT # 2:

This is another question from the same quiz.

Another dubious Query Query answer on W3Schools

+7
source share
2 answers

You are right, the first language could (should) be removed from all options.

According to HTML 4.01 Spec :

This attribute names the element. This name must be unique in the document.

Also, according to jQuery documentation for id selector :

Selects one item using the given id attribute

Under the hood, the selector uses document.getElementById("...") . Interestingly, the specification for this function reads:

No behavior is determined if more than one element has this identifier.

So, if you have two elements with the same id , the results of the function are unpredictable and depend on the browser.

Sidenote : W3Schools is not considered one of the best places to learn JavaScript / jQuery. A Well-Picked Alternative to JavaScript - MDC JavaScript Guide For jQuery, check out the tutorials page.

+15
source

Indeed, the behavior of document.getElementById ("...") is undefined if more than one element has this identifier.

However, as the w3schools site is consistently trying to indicate, jQuery's behavior in this situation is well defined. If more than one element has this identifier, then the first one will be selected.

No one condones more than one element having a specific identifier; this is still against the rules. However, unlike getElementById, jQuery has a certain reaction if this rule is violated.

+1
source

All Articles