1D0-635 · Question #56
Which of the following will correctly modify the value of an X/HTML element's attribute within the DOM?
The correct answer is D. setAttribute(AttributeName, NewValue). See the full explanation below for the reasoning.
Question
Which of the following will correctly modify the value of an X/HTML element's attribute within the DOM?
Options
- AgetAttribute(NewValue, AttributeName)
- BgetAttribute(AttributeName, NewValue)
- CsetAttribute(NewValue, AttributeName)
- DsetAttribute(AttributeName, NewValue)
How the community answered
(34 responses)- A18% (6)
- B3% (1)
- C6% (2)
- D74% (25)
Community Discussion
7D is the one you want. setAttribute takes the attribute name first, then the value you want to set, so setAttribute(AttributeName, NewValue) is the correct order and the correct method, because getAttribute only reads a value, it does not write one.
Dervla has it right, and if you burn in that one mnemonic, name before value, you will recognize the wrong-order distractors on the real exam without even blinking.
setAttribute takes the attribute name first, then the value, so D is the one to memorize: setAttribute("href", "newpage.html") for example. The options with getAttribute are there to trip you up since that method only reads a value and does not set anything, it just takes the attribute name as its single argument.
C is the classic trap because your brain sees "set" and thinks "yeah that modifies things," but then the arguments are flipped, so remember SAY THE NAME BEFORE YOU CHANGE IT, just like calling someone's name before handing them something, setAttribute(AttributeName, NewValue) is D and that is your answer.
D is the one you want. The DOM method setAttribute takes the attribute name as the first argument and the value you are setting as the second, so setAttribute("href", "https://example.com") is the correct call order. getAttribute is a read operation, it pulls an existing value out and does not accept a new value to write, so A and B are wrong on two levels, wrong method and wrong argument order. Mixing those two up is a classic test trap because they look similar, and the exam will absolutely try to flip the argument order on you in option C to see if you actually know the signature.
That one I can't argue for you, because B calls the wrong method entirely, and I would not want that stuck in your head test day. D is the one that will serve you.
Grace is actually pointing to D, which is exactly right, so you two are in agreement on this one.