Why does my Javascript function not work
I have this function that needs to search for h1, h2, and h3 tags from within a given element id and concatenate together all the innerhtml of any heading tags it finds. If none exist then it will return the title of the page instead.
But i have been getting errors - the latest is
Error: findHeading.getElementsByTagName is not a function
Source File:
Line: 234
Here is my function, can anyone help?
function heading(){
headings = ["h1","h2","h3"];
for(var h=0;h<headings.length;h++){
if(findHeading.indexOf(headings[h] != -1)){
strHeading += findHeading.getElementsByTagName(headings[h]).item(0).innerHTML;
}
}
if(strHeading!= ''){
return strHeading.replace(/ /g, "_");
}
else{
return mainTitle.replacec(/ /g, "_");
}
}
teammatt3 posted this at 15:26 — 23rd March 2009.
He has: 2,102 posts
Joined: Sep 2003
Where is findHeading defined?
This statement looks a little odd too:
if(findHeading.indexOf(headings[h] != -1)){
Why is a comparison operator in the indexOf Method?
decibel.places posted this at 16:06 — 23rd March 2009.
He has: 1,494 posts
Joined: Jun 2008
Can you post some of your HTML code?
This statement looks a little odd too:
if(findHeading.indexOf(headings[h] != -1))
should probably be
if(findHeading.indexOf(headings[h]) != -1)
but I sometimes have fewer problems with
if(findHeading.indexOf(headings[h])+1 != 0)
benf posted this at 18:09 — 23rd March 2009.
They have: 426 posts
Joined: Feb 2005
Yes you were correct i missed that it should be
if(findHeading.indexOf(headings[h]) != -1)
and findHeading was defined as the innerHTML of an element: so i changed this to just the reference to the element id.
Thats all fine, but where i have declared the headings array at the top of the page
var headings = new array()
i now get a error message from error console saying the below:
Error: array is not defined
Source File:
Line: 20
Good Value Professional VPS Hosting
decibel.places posted this at 19:45 — 23rd March 2009.
He has: 1,494 posts
Joined: Jun 2008
try
var headings = new Array();
(capital "A")
benf posted this at 18:56 — 24th March 2009.
They have: 426 posts
Joined: Feb 2005
for some reason the problem seems to be accessing the array from within a function.
I declared the array at the top of the page as you mentioned above and then defined some data to it like this:
var headings = new Array("h1","h2","h3");
that is ok, no error. But if i try and access the array from within a function the error console is telling me it is not defined.
Do i have to declare an array locally and not use it globally?
Good Value Professional VPS Hosting
teammatt3 posted this at 21:00 — 24th March 2009.
He has: 2,102 posts
Joined: Sep 2003
Can you post the entire javascript source code? It would make it a lot simpler to debug.
decibel.places posted this at 12:21 — 25th March 2009.
He has: 1,494 posts
Joined: Jun 2008
I don't think you use square brackets for the array definition (only for individual elements arrayname[0])
try
headings = ("h1","h2","h3");
coolman009 posted this at 12:56 — 1st April 2009.
They have: 5 posts
Joined: Mar 2009
I think you need to look at your brackets again....i don't think that the sequence of brackets is right..Looping is seen to me there..May be i am wrong but please take a note of that too...and do tell me if that works for you...
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.