ginerjm Posted March 21, 2012 Share Posted March 21, 2012 From a little hunting around I found this (no, not from W3): var TMS_evt_names = ["<?php echo join("\", \"", $TMS_evt_names); ?>"]; function chkEvent(i) { choice = document.getElementById("evt"+i).value.toUpperCase(); alert("in chkevent with "+choice); idx = TMS_evt_names.indexOf(choice); if (idx < 0) { alert("invalid event"); return false; } else { alert("found "+TMS_evt_names[choice]); return true; } } The array in the first line is being built, since I can see it in my browser. My problem is - when I run it and the function is called, IE tells me the the line with the indexof reference "doesn't support this property of method". I do note that in most examples using indexOf that the arg is a string constant, rather than a variable. How do I get the variable to work in this instance. I already tried using "indexOf(choice.value)" but that didn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/259434-finding-something-in-an-array/ Share on other sites More sharing options...
Adam Posted March 22, 2012 Share Posted March 22, 2012 That's because indexOf() is a string function, not an array function. You can't actually have an associative array object in JavaScript either, they're all numerically indexed -- the equivalent would require you to use just a plain object. Given you're not trying to assign any keys in the array definition though, I don't see how the choice variable comes into it? Quote Link to comment https://forums.phpfreaks.com/topic/259434-finding-something-in-an-array/#findComment-1330160 Share on other sites More sharing options...
ginerjm Posted March 22, 2012 Author Share Posted March 22, 2012 Interesting response. Thanks for pointing it out. Wonder why that came up in my google search for findgin something in an array? Quote Link to comment https://forums.phpfreaks.com/topic/259434-finding-something-in-an-array/#findComment-1330193 Share on other sites More sharing options...
ginerjm Posted March 22, 2012 Author Share Posted March 22, 2012 Solved this from a posting I found. var evts = TMS_evt_names.length; var globalfld = null; function isValidEvent(obj) { var choice = obj.value.toUpperCase(); if (choice=='') return true; var k = evts; while (k--) if (TMS_evt_names[k] === choice) return true; alert("' "+choice+" ' is not a valid Event code."); obj.value=''; setFocus(obj); return false; } function setFocus(elm) { globalfld = elm; setTimeout("globalfld.focus();globalfld.select()",100); } This takes the value of the incoming object (called via "onclick='isValidEvent(this)'" in my html and checks if the value is in my array. If not, it erases it from the screen and repositions the cursor back on that field. Quote Link to comment https://forums.phpfreaks.com/topic/259434-finding-something-in-an-array/#findComment-1330253 Share on other sites More sharing options...
Adam Posted March 22, 2012 Share Posted March 22, 2012 Ah sorry, there is an indexOf array method. I always thought there wasn't one :S .. Unfortunately though there's no support for it in IE prior to version 9. Using it does keep your code a lot cleaner though, so you could use the prototype object to define it for any browsers that don't already have it: if (Array.prototype.indexOf == null) { Array.prototype.indexOf = function(search, fromIndex) { var length = this.length; var index = (typeof(fromIndex) !== 'undefined') ? fromIndex : 0; for (index; index < length; index++) { if (this[index] == search) { return index; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/259434-finding-something-in-an-array/#findComment-1330278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.