views:

52

answers:

2

I have this:

var targetTitle = targetElement.getElementsByTagName('title').item(0);

Am i passing a plain string into targetTitle? or what am i passing exactly?

Which element would be item(1), item(2), etc... in here:

<title>title1</title>
<title><title2</title>

Does it just look for ALL the title tags on the page and return the 0,1,2/?

Would this return 'title2':

targetElement.getElementsByTagName(‘title’).item(1)
+1  A: 

You're getting the first <title> element from targetElement.

item(0): <title>title1</title>
item(1): <title><title2</title>

Note: those indexes starts with zero, not one.

Lekensteyn
is that a string or what is it?
I__
That's a HTMLElement object. To get the contents of it, use textContent or innerHTML, like `var title = targetTitle.innerHTML;`.
Lekensteyn
+1  A: 

The targetTitle will contain a reference to the DOM element object for the title tag.

The code only gets the first title tag, as there is only one in each document.

Guffa
what if there are two title tags as in my example
I__
Look at my edited answer, you're retrieving one of the title elements with `item(n)`, not both.
Lekensteyn
@I__: The browser is probably ignoring the second one, so there is really only one. Either way the code is only getting the first.
Guffa