1
<div id="demo" onclick="tasks("fade")">
   <div id="demo" onclick="tasks("glow")">
    <div id="demo" onclick="tasks("slide")">
       <script>
       function tasks(){
           fade = do fade;
           glow =  make glows;
           slide = move slide; 
       }
       </script>

How to use single function and declare multiple tasks and use them specific element like I have create above?

1
  • how to put multiple function on single onclick with diffrent tasks and same action Commented Oct 19, 2015 at 11:38

2 Answers 2

2

You can use variable which be available in your function:

<script>
 function tasks(effect){
 switch(effect){
  case "fade":
    // execute some code here;
    break;
  case "glow":
    // execute some code here;
    break;
  case "slide":
    // execute some code here;
    break;
}
   }
 </script>
Sign up to request clarification or add additional context in comments.

6 Comments

is there any way to declare like variable
This is a local variable which you can use in this function and this variable passed when onclick happened.
Ok thanks For help My problem is Resolved Both codes helped me thanks
mark your question as resolved - so other users will know that this answer already closed.
what if i want to triger more than one tasks
|
1

You can do it like this, although it can be done in many other ways.

function tasks(action){
 if(action === "fade"){
   console.log("Handle fade action here");
 }
 else if(action === "glow"){
    console.log("Handle glow action here");
 }
 else if(action === "slide"){
   console.log("Handle slide action here");
 }
}
<div id="demo" onclick="tasks('fade')">fade</div>
 <div id="demo" onclick="tasks('glow')">glow</div>
<div id="demo" onclick="tasks('slide')">slide</div>

Use switch:

function tasks(action){
  switch(action){
  
     case "fade": console.log("Fade action here");
                  break;
      
     case "glow": console.log("Glow action here");
                  break;
      
     case "slide": console.log("Slide action here");
                  break; 
      
     
     default :console.log("any other actions here")
  
  
  }
  
}
<div id="demo" onclick="tasks('fade')">Fade</div>
 <div id="demo" onclick="tasks('glow')">Glow</div>
<div id="demo" onclick="tasks('slide')">Slide</div>
<div id="demo" onclick="tasks('any other')">any other</div>
 
 

6 Comments

Thanks any other way to do this
You can also use switch statements like shown in the other answer
Thanks i will try this to is there any way to declare this like variable
What if i want multiple action like function(task task2)
Modify like this onclick="tasks('fade','glow')" and your function like this: function tasks(action1, action2){....}
|

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.