Jump to content

finding something in an array


ginerjm

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
            }
        }
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.