Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't my jQuery extension work? (TypeError: validStatus.arrayContains is not a function) 1

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
I wrote the following to enable/disable elements, and it works fine.

Code:
jQuery.fn.enable = function (enable) {
        var $this = $(this);
        if (enable) {
            $this.removeAttr("disabled");
        }
        else {
            $this.attr("disabled", "disabled");
        }
    };

Usage:
   var element = $(".someclass");
  element.enable(true);


I then attempted to write one for an array to determine if it contains a specific value, but on call i get error "validStatus.arrayContains is not a function"

Code:
jQuery.fn.arrayContains = function (value) {
    return jQuery.inArray(value, $(this)) !== -1;
};


Usage:
var validStatus = ["0", "5", "15"];
if (validStatus.arrayContains("10")) {
{

}

I don't understand why my first one works but the other does not.
Thanks for your time.
 
Hi

Because element is a jQuery object while validStatus is just a regular array.
JavaScript:
[gray]// either change the call :[/gray]
[b]if[/b] [teal]([/teal][highlight]$[teal]([/teal][/highlight]validStatus[highlight][teal])[/teal][/highlight][teal].[/teal][COLOR=orange]arrayContains[/color][teal]([/teal][i][green]"10"[/green][/i][teal])) {[/teal]
[teal]{[/teal]

[teal]}[/teal]

[gray]// or the declaration :[/gray]
[highlight]Array[/highlight][teal].[/teal][highlight][b]prototype[/b][/highlight][teal].[/teal]arrayContains [teal]=[/teal] [b]function[/b] [teal]([/teal]value[teal]) {[/teal]
    [b]return[/b] [highlight][b]this[/b][teal].[/teal][COLOR=orange]indexOf[/color][/highlight][teal]([/teal]value[teal]) !== -[/teal][purple]1[/purple]
[teal]};[/teal]

BTW, You might not need jQuery.


Feherke.
feherke.github.io
 
Thanks Feherke, that did not occur to me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top