0

Hello Stackoverflow, im kinda stucked :(

I am doing a code where I have some divs with specific ids like 1 and 2 and 3.

Now i want it to check if my number is part of an variable or an array, it should console.log it that, that div is locked. But I can't really figure out how to solve this? Can anyone help me?

I have tried this:

var f = getXY(e.pageX, e.pageY);
var obj = [1,2,3,4];
var arr = obj;

if ($.inArray(f, obj)) {
    console.log("Dette er låst");
} else {
    if (f) {
        $fields.removeClass('hover');
        $('#f' + f).addClass('hover');   
    }
}

I want to have more then one number in the array or variable.

Thanks guys.

2
  • 2
    @KevinB the second parameter is the array argument Commented Nov 26, 2012 at 16:43
  • @kinakuta Ah, so it is. KevinSteenHansen, Is getXY() returning an integer? Commented Nov 26, 2012 at 16:45

1 Answer 1

6

jQuery's inArray() returns the index where element was found. So your conditional should be:

if ($.inArray(f, obj) > -1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. But it didn't work. I can still click on the field. What i want to do, is so this man can't walk on field 1, 43, 12 exampel. See the link jsfiddle.net/6MxfQ/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.