2

I am working on a script and not sure why this isn't working

function moveIn($selector) {
if( $( $selector[left] ) != null ){
    $direction = 'left';
}else{
    $direction = 'right';
}

This is does work:

if( $( '#hello[left]' ) != null ){

This is essentially what I am trying to get spit out. It seems the brackets are causing the problem. How else would this be written? Tips for future coding? Thanks

Entire function:

function moveIn($selector) {
if( $( $selector + '[left]' ) != null ){
    $direction = 'left';
}else{
    $direction = 'right';
}
var animation = {};
    animation[$direction] = 0;
$($selector).animate(animation, 1500);
}
1
  • Tip, you shouldn't put $ in front of nearly all your variables; it may have some usefulness if it's a jQuery object. Commented Dec 21, 2012 at 3:30

1 Answer 1

3

Assuming you're passing "#hello" as the selector, you are then trying to get the left property of a string, which doesn't exist.

"#hello[left]", on the other hand, searches for an element with the ID hello and a left attribute.

They are not the same. Try $selector+"[left]"

Sign up to request clarification or add additional context in comments.

5 Comments

Yes that worked but my function still isn't working right. From what you see there should that work? if the selector has the left attribute fill the direction variable with the string left else make it right...?
Yes, but what are you doing with the $direction variabe?
Okay, what are the initial values of the left and right positions?
left is -260px right is -250px. they are two boxes that slide in from the left and right

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.