1

I'm writing a onclick listener for a button but receiving an

Uncaught TypeError: undefined is not a function error on line 4 of this code:

$("button#searchKeyword").click(function(){
    var searchTerm = $("input#searchBox").val();
    var i = myArr.indexOf(searchTerm)+1;
    var locationID = myArr[i].value();
    alert(searchTerm + " " + i + " " + locationID );   
});

The locationID is a 4 or 5 digit integer. And myArr is an array of strings and numbers (i.e. [string, number, string, number...]).

I've tried adding var $ = jQuery.noConflict(); after reading this. But none of the solutions there seem to work for me.

I'm wondering if the problem is a parsing issue? Do I need to parse i to an integer? If so how would I do that?

Community
  • 1
  • 1
DinosaurHunter
  • 652
  • 2
  • 9
  • 23
  • Which line gives the error and what is `myArr[i].value()`? Does your array elements have a method named `value`? – user0815 Mar 13 '15 at 17:50
  • The fourth line - ("error on line 4 of this code"). `myArr[i].value()` should be the `locationID`. I'm not sure what you mean by your other question. – DinosaurHunter Mar 13 '15 at 17:59
  • I mean the same as answered below. Native objects like strings and numbers that your array contains, don't have a method `value()`. But you call this method on these native elements. – user0815 Mar 13 '15 at 18:02

1 Answers1

1

If myArr is an array of strings and numbers, then you will get the error when you call myArr[i].value() because value is not defined for either numbers or strings.

You might also have an issue with i going beyond the bounds of the array because you add 1 to it. (That shouldn't cause this error message, but it's worth noting)

Wet Noodles
  • 755
  • 7
  • 15