0

//UPDATE: nevermind I'm dumb. It works, but I'll change it to if/else

I have an ajax function and on success it'll update some css styles. If valueExists is true, I'd like to add multiple css styles and I place these css styles in a function

.success(function(data) {
    data.valueExists ? multipleChanges($("#element")) : $("#element").addClass("classStyleTwo");      
});

//Outside of success function multiple css changes
function multipleChanges(element){
        $(element).addClass("classStyleTwo");
        $(element).css({color: "#1abc9c"});
}

The problem is that the ternary is in an async request, so the function multipleChanges is called instantly. I'm still a bit rusty on callback functions I tried placing multipleChanges inside another function like...

function wrapper (cb, element){
   return cb(element);
}

.success(function(data) {
    data.valueExists ? wrapper(multipleChanges, $("#element")) : $("#element").addClass("classStyleTwo");      
});
testinggnitset ser
  • 297
  • 1
  • 3
  • 17
  • 4
    As a side note, ternaries are mostly used to assign a value to a variable, not to perform operations – nicholaswmin May 02 '17 at 16:16
  • yeah I have multiple ternaries so I tried avoiding the if/else style. I looked at http://stackoverflow.com/questions/10323829/javascript-ternary-operator-example-with-functions, but you're right – testinggnitset ser May 02 '17 at 16:20
  • 1
    use ternaries where it makes more sense (to avoid duplicating assignment expressions). A ternary isn't by default better than `if else`. – nicholaswmin May 02 '17 at 16:25
  • Why ever you needed a hard to read code over easier to debug one? Think when yours get sized and found all the scripts are one lined :( I'm referring to "I have multiple ternaries" anyways – Chay22 May 02 '17 at 16:26

0 Answers0