Question:
JavaScript~?
anonymous
2008-07-16 03:25:52 UTC
Which of the following will get all the images of a html document?

a) var images=document.getElementsByTagName(images);
b) var images=document.getElementsById("img");
c) var images=document.getElementsByTagName("img");
d) var images=document.getElementsById("images")

I chose b) but it's wrong. I don't understand why...Can someone please explain to me?
-------------------------
Which of the following is a correct way to handle an event when a button is clicked?

a) onclick=alert('you click me');
b) onclick="alert("you click me");"
c) onclick="alert('you click me');"
d) onclick="alert(you click me);"

I chose a) but it's wrong. Can someone please explain to me?
-----------
Three answers:
piarvi
2008-07-16 03:45:45 UTC
You should use document.getElementsByTagName("img");

document.getElementById("id_name") is used to get an element through its id.



Getting all images in an html document means you should search all tags, thus you should use getElementsByTagName.



Regarding question number 2, the correct answer is letter c. The onclick keyword should be used as follows: onclick="script_here".

Also, When declaring strings inside a double quote it should be enclosed in apostrophes (').
AnalProgrammer
2008-07-16 10:52:04 UTC
1.

I think the answer is c because it is get Elements By Tag Name and I think that the tag name specified is IMG. I cannot tell because your text is not complete.



2.

c onClick requires the action to be in quotes. Javascript requires that you cannot use the same quote characters for different usages. so onClick='alert("you click me");' does the same thing but the otherway around. Of course HTML wants a double quote first. If you need a third level of quote marks then you have to excape the quote mark like \" or \'.



I hope this helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...