Shniper Shniper - 2 years ago 73
Javascript Question

How to determine if $(this) is in array on click event

I have a set of

values in 4 arrays. Each array will be assigned a text value for an h1 and a p that I haven't put in yet. Right now I'm just trying to get it to alert if one of the images in array graphicDesign is clicked. I tried using $.inArray


var graphicDesign = [$('#design'), $('#DD'), $('#SElogo')];
var webDesign = [$('#bootstrap'), $('#farm'), $('#pong'), $('#SE'), $('#dung')];
var programming = [$('#SE'), $('#dung'), $('#sacar')];
var other = [$('#firm')];

function categories() {
if ($.inArray(this, graphicDesign) > -1) {

Answer Source

You should not store DOM objects in an array and try to match them with $.inArray. Using ids or another attribute would be a better solution.

For example :

var graphicDesign = ['design', 'DD', 'SElogo'];

function categories(id) {
  if ($.inArray(id, graphicDesign) > -1) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download