Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  14] [ 1]  / answers: 1 / hits: 29373  / 13 Years ago, wed, may 11, 2011, 12:00:00

I'm developing this webapp for my school. The page is supposed to filter entries by the URL parameter "class". This works fine as far as I can tell, but when I try to change the filter it gives:



"TypeError: object is not a function".



What am I doing wrong?


<!DOCTYPE HTML>

<html>
<head>
<TITLE>Cancelled lessons</TITLE>

</head>
<body>

<script>
function filter(text){
text = text.toLowerCase();
for (i=0;i<lessonList.length;i++){
if(lessonList[i].innerHTML.toLowerCase().indexOf(text)==-1){
lessonList[i].style.display = "none";
}
else{
lessonList[i].style.display ="";
}
}
}

function gup( name )
{
name = name.replace(/[[]/,"\[").replace(/[]]/,"\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
</script>

<form>
Filter: <input type="text" id="filter" oninput="filter(document.getElementById('filter'))"/>
</form>

<div id="lessons">
<div class="entry"> MaA 11:00 C131 Ej NV3C</div>
</div>

<script>
var lessonList = document.getElementsByClassName("entry");
var filterField =document.getElementById("filter");
filterField.value = gup("class");
filter(filterField.value);
</script>
</body>
</html>

More From » javascript

 Answers
10

It looks like the oninput handler calls the filter function from the scope of the form (document.forms[0]) rather than globally. If you check the value of document.forms[0].filter it'll return the input tag. You just need to make sure that the function name is different than the input name/id.



This also means you don't need to get the input field by id every time, it's already scoped as this



<input type=text id=filterField oninput=filter(this.value)/>

[#92298] Monday, May 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;