Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Different way of writing multiple click functions

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 475
    Comment on it

    jQuery make it possible to interact with user by adding events to the web page, It includes many Events Methods like hide(), bind(), blur(), change(), click() and dbclick(). Here in this blog I just want to create one "click" function that includes all the events on a single click. If user wants to add many events to different divs on single click so they can use "On"  method that includes all the events into a single function, here I used "data-content" property also that will store the different values of the div into one variable named "data", then I used "if" condition for executing the different values of data-content the condition is checked into the if statement and then the every condition will be executed one by one.

    This is the simple code that will help you to understand better.

    <!DOCTYPE html>
    <script src=""></script>
        $("body").on("click", ".common", function(){
          var data = $(this).attr("data-content");
          if (data=="one") {
          if (data=="two"){
               $("#two").animate({left: '250px'});
         if (data=="three" ){
         if (data=="fourth"){
      .common{border:1px solid #000; background-color: green; color: #fff; padding: 10px 30px; cursor: pointer; width: 300px}
    <div class="common" id="one" data-content="one">Click This paragraph.</div>
    <div class="common" id="two" style="background:#98bf21;position:absolute; top:131px;"  data-content="two">Click last paragraph to animate it.</div>
    <div class="common" id="three"  data-content="three">Click to slide toggle this paragraph.</div>
    <div class="common" id="fourth"  data-content="fourth">Click to fade out paragraph.</div>

    Output:- For getting output you can download the below link.


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: