0

I can't seem to get this jQuery callback to work for me. Any ideas? BTW, please don't worry about the variables, I have a special project this is for.

$("#" + circle - 1).animate({
    opacity: 0,
    "margin-top": "50",
    height: '150px',
    width: '150px'
 }, 1000, function() {
    $("#" + circle).animate({
        opacity: 1,
        "margin-top": "50",
        height: '150px',
        width: '150px'
    }, 1000);
 });
4
  • 2
    What does "not working" mean? Can you create a jsFiddle.net example? Commented Jun 19, 2013 at 18:38
  • It does not run the callback or the main parent function... This will not work in a JSFiddle, because it's just part of a much larger program. Sorry! :( Commented Jun 19, 2013 at 18:38
  • Try replacing "margin-top" with marginTop: <-- and don't add quotes. Commented Jun 19, 2013 at 18:39
  • jsfiddle.net/x2Ddp/1 The code is fine, your problem is elsewhere Commented Jun 19, 2013 at 18:40

3 Answers 3

1

Either you have an issue in $("#" + circle - 1) which i am not sure what you are trying to deduct 1 from but I guess your id is a number which will fail with NAN during the calculation "#" + circle - 1 so change it to "#" + (circle - 1) and try:

$("#" + (circle - 1)).animate({ //<- here use () to seperate the number calculation otherwise it will be NAN.
    opacity: 0,
    "margin-top": "50",
    height: '150px',
    width: '150px'
 }, 1000, function() {

    $("#" + circle).animate({
        opacity: 1,
        "margin-top": "50",
        height: '150px',
        width: '150px'
    }, 1000);
 });
Sign up to request clarification or add additional context in comments.

Comments

1

I would assume that your code is working just fine and there IS something wrong with your variables. If $('#' + circle) doesn't find anything then nothing else will happen. Here is your code working, slightly modified.

http://jsfiddle.net/qbtDj/1/

$("#mydiv").animate({
  opacity: 0,
  "margin-top": "50",
  height: '150px',
  width: '150px'
 }, 1000, function() {
  $("#mydiv" ).animate({
  opacity: 1,
  "margin-top": "50",
  height: '150px',
  width: '150px'
 }, 1000);
 });

Comments

1

For me 1 is not a valid ID, use item1 and item2 too for instance. Like this (http://jsfiddle.net/balintbako/XSGN8/):

var circle = 2;
$("#item" + (circle - 1)).animate({
    opacity: 0,
    "margin-top": "50",
    height: '150px',
    width: '150px'
}, 1000, function () {
    $("#item" + circle).animate({
        opacity: 1,
        "margin-top": "50",
        height: '150px',
        width: '150px'
    }, 1000);
});

3 Comments

using number as id is valid with new html specs. Also btw this was my answer before your edit itself. :)
I wont't edit this time, because this fiddle is really working with all the counting and that:) And it was not working in IE10 with id="1"...
Don't worry, guys! I fixed it! Thanks, and goodbye numerical ID's!

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.