0

I am developing an app where users can post. I want to make it so that when a post's id is hashed in the url, the post flashes. To accomplish this, here is my code:

function getHash() {
  var hash = window.location.hash;
  return hash; 
}

if (getHash()) { 
    $(getHash()).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300);
}

Now, I know this code is run b/c if I put an alert('in the if statement');, it works whenever there is a hash in the url. I also know this part works:

    $(getHash()).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300);

because if I run it in the console, the post flashes. What is going wrong that is not allowing this to work?

2
  • put your getHash function in document ready ($(document).ready()) Commented Sep 20, 2012 at 17:38
  • this is not the problem since the getHash() function executes just fine in the if (getHash()) { line. Commented Sep 20, 2012 at 17:47

1 Answer 1

1

try your code like this

function getHash() {
  var hash = window.location.hash;
  return hash; 
}

$(document).ready(function(){
  if (getHash()) { 
    $(getHash()).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300).fadeOut(300).fadeIn(300);
  }
})

I think since your element does not exist when your script is executing. Document ready works when whole document is prepared, and all your elements are in the place. Since everything seems fine, I think this will work

Thanks

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

Comments

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.