HEYHEY
HEYHEY

Reputation: 533

jQuery combining functions

So, I have following functions:

function DIV(a,b){
   return '<div class="'+ a +'">'+ b +'</div>';
}
function IMG(a,b,c){
    return '<img class="'+ a +'" src="'+ b +'" alt="'+ c +'">';
}
function HREF(a,b,c){
    return '<a class="'+ a +'" href="'+ b +'">'+ c +'</a>';
}
function SPAN(a,b){
    return '<span class="'+ a +'">'+ b +'</span>';  
}

Is there a way to combine these functions to simplify them into one function.

Upvotes: 0

Views: 22

Answers (1)

YaBCK
YaBCK

Reputation: 3029

You could use a switch statement like this following example I've made to show you:

function TestFunction(tag, a, b, c)
{
  switch (tag)
  {
      case "div":
      case "span": 
        return '<' + tag + ' class="'+ a +'">'+ b +'</' + tag + '>';
      break;
      
      case "img":
        return '<' + tag + ' class="'+ a +'" src="'+ b +'" alt="'+ c +'">';
      break;
                
      case "a":
        return '<' + tag + ' class="'+ a +'" href="'+ b +'">'+ c +'</a>';
      break;

      default:
          // Default value.
      break;
  }  
  
}

console.log(TestFunction("div", 1, 1, 1));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Upvotes: 2

Related Questions