title: Trouble setting JavaScript variable of object using ternary operator
body: <p>Hi Guys I’m having trouble trying to set a variable (val) to be one of 2 possible object attributes. The below code explains what I’m trying to do.</p>
  
  <pre><code>function myFnct(elem, imgSrcType) {
    var val = imgSrcType == "bg" ? elem.style.backgroundImage : elem.src;  
    val  = 'image.jpg'  
  }
  </code></pre>
  
  <p>I’m using a ternary operator to try and avoid having to write:</p>
  
  <pre><code>if (imgSrcType === "bg") {
      elem.style.backgroundImage = "url('image.jpg')";
  }
  else {
      elem.src = "image.jpg";
  }
  </code></pre>
  
  <p>Basically the ‘val’ variable is not getting set correctly as I guess its something to do with elem object. I’m trying to avoid using the if statement as I will need to use it a few times within the function. And I’m trying to keep is as DRY as possible.</p>
  
  <p>Any help getting it to work with the ternary operator method would be awesome!</p>