0

im trying to create this function in javascript but its not working, im a bit new to js, so i don't what im really doing wrong here.

the code:

<div id="test">TESTING</div>

JS:

function animateDiv(div){

var text = $('#' + div + '"').text();

var doAnimate = function() {
    $('span').each(function() {
        var that = $(this);
        setTimeout(function() {
            that.animate({ fontSize: "90px" }, 1500 )
                .animate({ fontSize: "50px" }, 1500 );
        },that.index()*100);
    });
}

$('#' + div + "'").html('');

for(i=0; i<text.length; i++) {
  $('#' + div + "'").append('<span>'+text[i]+'</span>');
    if(i==text.length-1) doAnimate();
  }

}
  // using the function here to run animation on div test from html  
 animateDiv(test);

the jsfiddle is here: http://jsfiddle.net/aA8Un/3/

5
  • its more this function is not working i dont know why sort of question. i clarified this at the top. Commented Jul 24, 2012 at 13:05
  • define "not working". Your fiddle seems fine. Commented Jul 24, 2012 at 13:09
  • the version I saw was working, but it was different code to what you had posted here. Commented Jul 24, 2012 at 13:17
  • its noot a problem, thanks for helping, the question has been answered now!! Commented Jul 24, 2012 at 13:18
  • please be more careful next time. The code posted here had obvious flaws, but your question was lousy. Commented Jul 24, 2012 at 13:19

4 Answers 4

1

This works now

function animateDiv(div){

var text = $('#' + div.id).text();

var doAnimate = function() {
    $('span').each(function() {
        var that = $(this);
        setTimeout(function() {
            that.animate({ fontSize: "90px" }, 1500 )
                .animate({ fontSize: "50px" }, 1500 );
        },that.index()*100);
    });
}

$('#' + div.id).html('');

for(i=0; i<text.length; i++) {
  $('#' + div.id).append('<span>'+text[i]+'</span>');
    if(i==text.length-1) doAnimate();
  }

}

 animateDiv(test);

Actually you were trying to concatenate a string and an object by this $("#"+div), which is wrong, you should do this $("#"+div.id) which is legal.

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

Comments

1

You have to call the function using single/double quotes like

animateDiv("test");

instead of

animateDiv(test);

And remove '"' from your code everywhere

So Make it $('#' + div + '"') to $('#' + div)

Working Demo

Comments

0
var text = $('#' + div + '"').text();

replace with

var text = $('#' + div).text();

Comments

0
  1. animateDiv('test')

  2. subsitute $('#' + div + '"') with $('#' + div)

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.