0

Hey all this is the first time I've ran into this problem with javascript/jquery. Below I have a part of code that I am trying to place an IF/Than else statement in order order to decide what type of class I need to place into the DIV it creates:

Select3.Templates = {
   dropdown: function (options) {
      var extraClass = (options.dropdownCssClass ? ' ' + options.dropdownCssClass : ''),
      searchInput = '';

      if (options.showSearchInput) {
         extraClass += ' has-search-input';

         var placeholder = options.searchInputPlaceholder;
         searchInput = (
            '<div class="select3-search-input-container">' +
            '<input class="select3-search-input"' + (placeholder ? ' placeholder="' + escape placeholder) + '"'
            : '') + '>' +
            '</div>'
         );
      }
      return (
         '<div class="select3-dropdown' + extraClass + '">' + searchInput + '<div class="select3-results-container"></div>' +
         '</div>'
      );
      },
      loading: function () {
         return '<div class="select3-loading">' + Select3.Locale.loading + '</div>';
      },
      loadMore: function () {
         return '<div class="select3-load-more">' + Select3.Locale.loadMore + '</div>';
      },
      multipleSelectInput: (
         '<div class="select3-multiple-input-container">' +
         '<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" class="select3-multiple-input">' +
         '<span class="select3-multiple-input select3-width-detector"></span><div class="clearfix"></div>' +
         '</div>'
      ),
      [ect...ect..]

I'm trying to insert my if than else statement into the multipleSelectInput area but I am unsure how to go about doing that since it seems to be some type of array (or object array) or something I don't know what it's called :)?

If I place my if than else statement above the if (options.showSearchInput) { will that work within the multipleSelectInput area?

What I need to changed within the multipleSelectInput is:

'<div class="select3-multiple-input-container">' +

to

'<div class="select3-multiple-input-containerADMIN">' +

depending on the if statement else path.

So how can I insert the if than else statement where I need to?

1 Answer 1

1

You're defining an object literal (Select3.Templates), and multipleSelectInput seems to be a string property. You can't put statements into an object literal directly, but you can calculate a variable ahead of time and then set the property value to the value of that variable.

Example:

var multiSelectClass = "select3-multiple-input-container";
if (something == "admin")
  multiSelectClass += "ADMIN";


Select3.Templates = {
  // stuff
  multipleSelectInput: '<div class="' + multiSelectClass + '"....."
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep that did what I needed it to do! Thanks for the knowledge James!

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.