0

For an element defined as display:none in css I am trying to run a function which tries to display the element using .show(), code below:

CSS

.element {
  position: absolute;
  display: none;
  left: 0;
  top: 0;
}

HTML

<div class="element">Some content here</div>
<div class="element">Some content here</div>

jQuery

var c = $('.element');

c.each(function () {
  c.css({
    'left': dleft + 'px'
  });

  c.css({
    'top': dtop + 'px'
  });

  c.setTimeout(function () {
    c.show(1000);
  }, sduration);

All the variables are getting populated I have checked by alerting all of them and given them default values as well, but somehow the element is not being shown after the timeout.

1
  • 2
    You can't use setTimeout like that. Commented Sep 11, 2012 at 15:38

3 Answers 3

0

There seems to be two problems in the code you have written. you have not closed the .each() loop .. Also setTimeout is a global function.. .. Check this

    var dleft = 40;
var dtop = 40;
var sduration = 1000;
var c = $('.element');

c.each(function() {
    c.css({
        'left': dleft + 'px',
        'top': dtop + 'px'
    });
});
setTimeout(function() {
    c.show(1000);
}, sduration);​

Check this FIDDLE

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

Comments

0

setTimeout is a global function.

var c = $('.element');

c.each(function () {
  c.css({ 'left': dleft + 'px', 'top': dtop + 'px' });

  setTimeout(function () {
    c.show(1000);
  }, sduration);
});

Demo: http://jsfiddle.net/S7nw3/

Comments

0

If you need to delay when the elements are being shown, you can also make use of the jQuery .delay() function.

c.each(function() {
    c.css({
        'left': dleft + 'px',
        'top': dtop + 'px'
    });
});

c.delay(1000).show();

2 Comments

if you're trying to animate the showing of the element, you pass that into the .show() method itself, so instead of the delay, you can use .show('slow')
Thanks, but i got that by using .animate and the callback using complete parameter....

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.