1

I have the following code:

Javascript:

 <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
 <script type="text/javascript">
 $(function() {
 var transition = 'slow';
 var target = $('#somediv');
 target.delay(5000).fadeIn();
 });
 </script>

HTML:

 <div id="somediv">Some Content</div>
 <div id="second_div">Some second content</div>
 <div id="third_div">Some third content</div>

So as you can see the above function will work with #somediv but how do I reuse the same function for #second_div and #third_div?

I am new to Javascript so please pardon me if this is a stupid question. Thanks!

2
  • @zoltanToth thanks but the thing is i want to time each div separately... Commented Dec 4, 2012 at 19:45
  • 4
    While most people here discourage links to w3schools, I think you're at such an early level that you could benefit from this. Once you think you know what you're doing, try MDN instead as a more thorough and reliable reference. Commented Dec 4, 2012 at 19:52

5 Answers 5

3
<script type="text/javascript">
 $(function(){
    fade($("#yourElement"), 5000)
 });

 function fade(element, delay) {
     var transition = 'slow';
     var target = element;
     target.delay(delay).fadeIn();
 }
</script>

Then you can pass the object itself wherever you want, like:

fade($("#yourOtherElement"), 1000);

Note that you can pass different time delays, or can optionally even set a default time delay:

function fade(element, delay) {
    if(typeof delay === 'undefined') delay = 5000;

     var transition = 'slow';
     var target = element;
     target.delay(delay).fadeIn();
 }    

Then if you wanna use the default time delay just pass the first argument:

fade($("#yourOtherElement")); //will have 5000ms as delay time
Sign up to request clarification or add additional context in comments.

Comments

2

Use an argument:

function stuff(idstr) {
   var transition = 'slow';
   var target = $('#'+idstr);
   target.delay(5000).fadeIn();
};

$(function() {
   stuff('somediv');
   stuff('second_div');
   stuff('third_div');
});

If you want to change the timing delay, pass a second argument for that number:

function stuff(idstr, del) {
   var transition = 'slow';
   var target = $('#'+idstr);
   target.delay(del).fadeIn();
};

$(function() {
   stuff('somediv',5000);
   stuff('second_div',2000);
   stuff('third_div',3000);
});

http://jsfiddle.net/mblase75/cnH7d/

1 Comment

Thanks. I tried it as you have it but it wouldn't work. Besides changing the div# is there anything else that i have to modify for it to work on mine?
1

If you have a containing div, you could make a jquery selector like this to get all 3 divs

  <div id="somediv">Some Content</div>
  <div id="second_div">Some second content</div>
  <div id="third_div">Some third content</div>

js:

$(function() {
 var transition = 'slow';
 var target1 = $('#somediv');
 var target2 = $('#second_div');
 var target3 = $('#third_div');
 target1.delay(5000).fadeIn();
 target2.delay(target2Time).fadeIn();
 target3.delay(target3Time).fadeIn();
 });

4 Comments

Thanks but i want different time delay for different divs.
I see. In that case, you can just get all 3 separately and set them accordingly. See the edit I made.
@KPO You really should have put that in the original question, then.
Thanks @shredder! Worked like a charm.
1

Pass it in as a parameter:

<script type="text/javascript">
    function divFunc(divId, timeDelay) {
        var transition = 'slow';
        var target = $("#" + divId);
        target.delay(timeDelay).fadeIn();
    }
</script>

3 Comments

Thanks but I ahve to use different time delay for different divs.
Thanks but I am a bit confused as to how to call the function for each div then. Again I am sorry if this is obvious, i am a bit new to this.
When you want to call the function, just do: divFunc('somediv', 3000);, just replacing the div and the time with what you want. Call it for each div. When do you want to call this function?
1
function fade(selector){
    var target = $(selector);
    target.delay(5000).fadeIn();
}

fade('#div1') , fade('#div2') you can pass any jquery selector as a input to this function to fade it.

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.